EF 4.3 - 使用TPT仍在基础上生成Discriminator字段

时间:2012-08-16 23:03:06

标签: c# ef-code-first entity-framework-4.3

我在使用带有Code First的EF 4.3时遇到TPT问题。我有以下课程:

public class Section
{
    public int Id { get; set; }

....

    public int SurveyId { get; set; }
    public virtual Survey Survey { get; set; }
}

public class IntroductionSection : Section
{
    public string ExampleText { get; set; }
}

public class QuestionSection : Section
{
    public int ExampleNumber { get; set; }
}

使用Fluent API映射如下:

modelBuilder.Entity<Section>().Map(m => m.ToTable("Section"));
modelBuilder.Entity<Section>().HasRequired(m => m.Survey).WithMany(s => s.Sections).HasForeignKey(t => t.SurveyId);

modelBuilder.Entity<IntroductionSection>().Map(m => m.ToTable("Introduction"));
modelBuilder.Entity<QuestionSection>().Map(m => m.ToTable("Question"));

所有似乎都运行良好,并且创建了三个表并填充了播种时我期望的数据 - 唯一的问题是我仍然在'Section'表中生成'Discriminator'字段,因为我会使用TPH。任何想法为什么会这样,我怎么能摆脱它?

我尝试使用简单的Animal / Cat / Dog类型复制问题,并且(烦人地)工作正常!

1 个答案:

答案 0 :(得分:0)

我刚刚解决了我的问题。在我的例子中,我的模型有一个抽象的基类。所有模型都继承自基类。

我一直使用TPT但突然在数据库中有一个鉴别器。我花了一些时间试图摆脱鉴别器,最后注意到我有一个使用鉴别器从模型中得到的视图模型。

我将[NotMapped]属性添加到我的视图模型类中,并且不再使用鉴别器。

相关问题