Map Discriminator Column to class Property Entity Framework Core 2.2.0

c# entity-framework-core


I am failing to generate Migration for Entity Framework Core (v. 2.2.0)

I have a class "Question" which is inherited by class "MultipleChoiceQuestion" & "MatchColumns". I am trying to build a TPH (Table per Hierarchy) inheritance using Entity Framework Core. My base class (Question) has a property "QuestionType" to distinguish between child classes. The "QuestionType" property is an Enum. I don't want EF to generate a Discriminator column but use the property "QuestionType" as Discriminator column.

To create a proper migration, I am using the following code in dbcontext class:

protected override void OnModelCreating(ModelBuilder modelBuilder)
    modelBuilder.Entity<Question>(b => b.HasDiscriminator(x => x.QuestionType).HasValue<MultipleChoiceQuestion>(QuestionType.MultipleChoiceQuestion).HasValue<MatchColumns>(QuestionType.MatchColumns));

    modelBuilder.Entity<Question>().Property(p => p.QuestionType).HasMaxLength(100).HasColumnName("QuestionType");

But when I run "Add-Migration" I get the following error:

The entity type 'Question' is part of a hierarchy, but does not have a discriminator value configured.

Can't figure out why the configuring code is failing. I looked at the following documentation from Microsoft:

Any pointers why the configuration is incomplete/breaking?


2/20/2020 5:33:59 AM

Accepted Answer

Either Question should be abstract. Or it should be one of the possible record types, with a discriminator value.

2/20/2020 5:37:11 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow