Reputation: 15
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: https://learn.microsoft.com/en-us/ef/core/modeling/inheritance#discriminator-configuration
Any pointers why the configuration is incomplete/breaking?
TIA.
Upvotes: 0
Views: 4268
Reputation: 11090
Either Question
should be abstract. Or it should be one of the possible record types, with a discriminator value.
Upvotes: 3