我有3张表如下:
http://images.cnblogs.com/cnblogs_com/guozili/390921/o_ef.png
可以支持如下:
class Product
int Id {get;set;}
string Name {get;set;}
List<Picture> Picture {get;set;}
class Picture
...
[ForeignKey("Product.ProductId".....when type=1)]
int ModelId {get;set;}
我希望它可以通过映射属性或“.ToTable()... WithMany(c =&gt; ..)。Map(.....” 这样做了吗?
答案 0 :(得分:0)
这可以通过TPH策略实现(但不完全如此),例如
public class Picture {
public Int32 Id {get; set;}
public String URL {get; set;}
}
public class ProductPicture : Picture {
public virtual Product {get; set;}
}
public class CompanyPicture : Picture {
public virtual Company {get; set;}
}
并在构建模型时
protected override OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Entity<Picture>()
.Map<ProductPicture>(e => e.Requires("Type").HasValue(1))
.Map<CompanyPicture>(e => e.Requires("Type").HasValue(2))
}
在这种情况下,您将拥有一个表格: