简体   繁体   English

将鉴别器列映射到类 Property Entity Framework Core 2.2.0

[英]Map Discriminator Column to class Property Entity Framework Core 2.2.0

I am failing to generate Migration for Entity Framework Core (v. 2.2.0)我无法为 Entity Framework Core (v. 2.2.0) 生成迁移

I have a class "Question" which is inherited by class "MultipleChoiceQuestion" & "MatchColumns".我有一个类“问题”,它由类“MultipleChoiceQuestion”和“MatchColumns”继承。 I am trying to build a TPH (Table per Hierarchy) inheritance using Entity Framework Core.我正在尝试使用 Entity Framework Core 构建 TPH(每个层次结构的表)继承。 My base class (Question) has a property "QuestionType" to distinguish between child classes.我的基类 (Question) 有一个属性“QuestionType”来区分子类。 The "QuestionType" property is an Enum. “QuestionType”属性是一个枚举。 I don't want EF to generate a Discriminator column but use the property "QuestionType" as Discriminator column.我不希望 EF 生成鉴别器列,而是使用属性“QuestionType”作为鉴别器列。

To create a proper migration, I am using the following code in dbcontext class:为了创建正确的迁移,我在 dbcontext 类中使用了以下代码:

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:但是当我运行“Add-Migration”时,出现以下错误:

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://docs.microsoft.com/en-us/ef/core/modeling/inheritance#discriminator-configuration我查看了 Microsoft 的以下文档: https : //docs.microsoft.com/en-us/ef/core/modeling/inheritance#discriminator-configuration

Any pointers why the configuration is incomplete/breaking?任何指示为什么配置不完整/破坏?

TIA. TIA。

Either Question should be abstract.任何一个Question都应该是抽象的。 Or it should be one of the possible record types, with a discriminator value.或者它应该是可能的记录类型之一,具有鉴别器值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM