Skip to content

EFCore 8.0.7 Breaks Discriminator Generated Code #34563

@WynieCronje

Description

@WynieCronje

File a bug

More Info:

I have tested this by downgrading each version. The expected behavior worked in 8.0.6 and started breaking in 8.0.7. I have a suspicion that it is related to the PR #33622 that fixed the isssue #33605 in commit: f08ecf3

Include your code

PatientLocator.cs

public abstract class PatientLocator : DomainEntity
{
   public int Discriminator { get; private set; }
   ....
}

EntityTypeConfiguration.cs

b.HasDiscriminator(p => p.Discriminator)
   .HasValue<PatientAddress>(0)
   .HasValue<PatientPhone>(1)
    .HasValue<PatientEmail>(2);

b.HasOne(p => p.Patient)
   .WithMany(p => p.Locators)
    .IsRequired()
    .OnDelete(DeleteBehavior.Cascade);

Snapshot on 8.0.6

 modelBuilder.Entity("DOH.CARS.Domain.PatientDirectory.PatientLocator", b =>
                {
                    b.Property<Guid>("Id")
                        .HasColumnType("uniqueidentifier")
                        .HasColumnName("PatientLocatorId");

                    b.Property<int>("Discriminator")
                        .HasColumnType("int");

                    b.Property<Guid>("PatientId")
                        .HasColumnType("uniqueidentifier");

                    b.Property<byte[]>("Timestamp")
                        .IsConcurrencyToken()
                        .ValueGeneratedOnAddOrUpdate()
                        .HasColumnType("rowversion");

                    b.HasKey("Id");

                    b.HasIndex("PatientId");

                    b.ToTable("PatientLocator");

                    b.HasDiscriminator<int>("Discriminator");

                    b.UseTphMappingStrategy();
                });

Snapshot on 8.0.7

            modelBuilder.Entity("DOH.CARS.Domain.PatientDirectory.PatientLocator", b =>
                {
                    b.Property<Guid>("Id")
                        .HasColumnType("uniqueidentifier")
                        .HasColumnName("PatientLocatorId");

                    b.Property<int>("Discriminator")
                        .HasColumnType("int");

                    b.Property<Guid>("PatientId")
                        .HasColumnType("uniqueidentifier");

                    b.Property<byte[]>("Timestamp")
                        .IsConcurrencyToken()
                        .ValueGeneratedOnAddOrUpdate()
                        .HasColumnType("rowversion");

                    b.HasKey("Id");

                    b.HasIndex("PatientId");

                    b.ToTable("PatientLocator");

                    b.HasDiscriminator();

                    b.UseTphMappingStrategy();
                });

The Diff:
image

Include stack traces

When running the add-migration. It fails to compile the snapshot with below error:
Ignore the line numbers, they seem incorrect. I know the above mentioned changes causes the issue. If I manually change to old value, it all works again

System.NullReferenceException: Object reference not set to an instance of an object.
   at DOH.CARS.Persistence.Migrations.AppDbContextModelSnapshot.<>c.<BuildModel>b__0_77(EntityTypeBuilder b) in /work/CARS - Clinical Appointment and Referral System/DOH.CARS.Persistance/Migrations/AppDbContextModelSnapshot.cs:line 4374
   at Microsoft.EntityFrameworkCore.ModelBuilder.Entity(String name, Action`1 buildAction)
   at DOH.CARS.Persistence.Migrations.AppDbContextModelSnapshot.BuildModel(ModelBuilder modelBuilder) in /work/CARS - Clinical Appointment and Referral System/DOH.CARS.Persistance/Migrations/AppDbContextModelSnapshot.cs:line 4358
   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)

Include provider and version information

EF Core version: Breaks in 8.0.7,8.0.8
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 8.0)
Operating system: Both Mac and Windows
IDE: (e.g. Both Visual Studio Code and Visual Studio 2022 17.8.5)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions