【发布时间】:2012-08-16 23:03:06
【问题描述】:
我在使用带有 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”字段,就像我使用的那样总磷。任何想法为什么会这样以及我如何摆脱它?
我尝试使用简单的 Animal/Cat/Dog 类型类来复制该问题,并且(令人讨厌)工作正常!
【问题讨论】:
-
您是否首先使用 TPH 并让迁移将其更改为 TPT?
-
我在 Entity Framework 5.0.0 中遇到了同样的问题。就我而言,它是使用 TPT 的多级继承。您解决了这个问题吗?
-
我也有类似的问题。我有一个抽象基类,许多派生类中的一个有一个鉴别器,到目前为止我还没有运气。
标签: c# ef-code-first entity-framework-4.3