2013-04-18 66 views
0

我有以下傳統的表結構(簡化了此篇) enter image description hereEF 4.3流利映射中級表TPT

以下是我在配置所述實體微弱嘗試:

public class EntityConfiguration : EntityTypeConfiguration<Entity> { 
public EntityConfiguration() { 
    ToTable("Entity"); 
    HasKey(x => x.Id); 
    Property(x => x.Id) 
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 

    HasMany(x => x.TypeOneUpdateBlacklist) 
    .WithMany() 
    .Map(x => { 
     x.ToTable("UpdateBlacklist"); 
     x.MapLeftKey("EntityId"); 
     x.MapRightKey("UpdateId"); 
    }); 

    HasMany(x => x.TypeTwoUpdateBlacklist) 
    .WithMany() 
    .Map(x => { 
     x.ToTable("UpdateBlacklist"); 
     x.MapLeftKey("EntityId"); 
     x.MapRightKey("UpdateId"); 
    }); 
} 

配置使得這個錯誤:

模式'dbo'和表'UpdateBlacklist'的EntitySet'EntityBlacklistUpdate'已經被定義。每個EntitySet必須引用一個唯一的模式和表。

有沒有配置這個?在此先感謝

+0

這似乎是不可能的。我能想到的唯一可行的解​​決方案是兩個不同的表格TypeOneUpdateBlacklist和TypeTwoUpdateBlacklist。 – Dan

回答

0

你應該能夠與基型Update創建許多一對多映射:

public class EntityConfiguration : EntityTypeConfiguration<Entity> { 
    public EntityConfiguration() { 
    ToTable("Entity"); 
    HasKey(x => x.Id); 
    Property(x => x.Id) 
     .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 

    HasMany(x => x.Updates) 
     .WithMany() 
     .Map(x => { 
      x.ToTable("UpdateBlacklist"); 
      x.MapLeftKey("EntityId"); 
      x.MapRightKey("UpdateId"); 
    }); 
} 

然而,這將要求您Entity類並不僅僅有一個導航集合Updates基本類型,而不是兩個派生類型的兩個導航集合。它是唯一可能的,如果數據庫架構真正代表一個繼承模型,即給定Update行可要麼有相關TypeOneUpdate一個TypeTwoUpdate一行,雙方從未。如果可以同時擁有這兩種關係,則無法將其與TPT進行映射,但必須創建一對一的關係。