Skip to content

Generating new ModelSnapshot removes type and name for HasDiscriminator leading to null reference exception in 8.0.7Β #34201

@SandstromErikTS

Description

@SandstromErikTS

When I try to add a new migration which generates a new ModelSnapshot, type and name is removed from HasDescriminator in modelBuilder.Entity. I have discriminator configured in a seperate configuration file.

For example, in my configuration file I have

internal class MyEntityBaseConfiguration : IEntityTypeConfiguration<MyEntityBase>
{
	public void Configure(EntityTypeBuilder<MyEntityBase> builder)
	{
		builder.HasKey(x => x.Id);

		builder.HasDiscriminator<byte>("Discriminator")
			.HasValue<MyEntityBase>(0)
			.HasValue<MyFirstEntity>(1)
			.HasValue<MySecondEntity>(2);
	}
}

In version 8.0.6 this results in the following when the ModelSnapshot is generated

modelBuilder.Entity("MyNamespace.MyEntityBase", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    b.Property<byte>("Discriminator")
                        .HasColumnType("tinyint");

                    b.HasKey("Id");

                    b.ToTable("MyEntityBases", "dbo");

                    b.HasDiscriminator<byte>("Discriminator").HasValue((byte)0);

                    b.UseTphMappingStrategy();
                });

But now the generated snapshot looks like this, which results in a null reference exception when I try to add a new migration

modelBuilder.Entity("MyNamespace.MyEntityBase", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int");

                    b.Property<byte>("Discriminator")
                        .HasColumnType("tinyint");

                    b.HasKey("Id");

                    b.ToTable("MyEntityBases", "dbo");

                    b.HasDiscriminator().HasValue((byte)0);

                    b.UseTphMappingStrategy();
                });

Callstack:

System.NullReferenceException: Object reference not set to an instance of an object.
   at MyModelSnapshot.<>c.<BuildModel>b__0_201(EntityTypeBuilder b) in C:\xxx.MyModelSnapshot.cs:line XXX
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
   at MyModelSnapshot.BuildModel(ModelBuilder modelBuilder) in C:\xxx.MyModelSnapshot.cs:line XXX
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.CreateModel()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSnapshot.get_Model()
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

Provider and version information

EF Core version: 8.0.7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.10

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions