2013-04-18 51 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进行映射,但必须创建一对一的关系。