2012-08-16 36 views
1

我在Code First使用EF 4.3時遇到了TPT問題。我有以下類: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; } 
} 

映射使用流利的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一樣。任何想法,爲什麼會這樣,我如何擺脫它?

我試圖複製使用簡單的動物/貓/狗類型的問題,(煩人)工作正常!

+0

您是否首次使用TPH並讓遷移將其更改爲TPT? – 2012-08-17 07:11:34

+0

我在實體框架5.0.0中有這個相同的問題。就我而言,它是使用TPT的多級繼承。你解決了這個問題嗎? – 2013-02-18 22:55:48

+0

我有類似的問題。我有一個抽象基類,許多派生類中的一個有一個鑑別器,我試圖擺脫目前爲止沒有運氣。 – Jonny 2016-10-20 07:51:59

回答

0

我剛解決了我的問題。在我的例子中,我的模型有一個抽象基類。所有模型都從基類繼承而來。

我一直使用TPT,但突然間在數據庫中出現了鑑別器。我花了一些時間試圖擺脫鑑別器,並最終注意到我有一個從鑑別器模型派生的視圖模型。

我將[NotMapped]屬性添加到我的視圖模型類中,鑑別器不再有了。

相關問題