【问题标题】:EF 4.3 - using TPT still generating Discriminator field on baseEF 4.3 - 使用 TPT 仍然在基础上生成鉴别器字段
【发布时间】: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


【解决方案1】:

我刚刚解决了我的问题。就我而言,我的模型有一个抽象基类。从基类继承的所有模型。

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

我在我的视图模型类中添加了 [NotMapped] 属性,并且不再有鉴别器了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多