Entity framework 由两个外键组成的单个集合?

Entity framework 由两个外键组成的单个集合?,entity-framework,ef-code-first,Entity Framework,Ef Code First,假设我有以下实体: public partial class Store { public Store() { this.ConglomeratesByField1 = new HashSet<Conglomerate>(); this.ConglomeratesByField2 = new HashSet<Conglomerate>(); } [Key] public int ID_Store {

假设我有以下实体:

public partial class Store
{
    public Store()
    {
        this.ConglomeratesByField1 = new HashSet<Conglomerate>();
        this.ConglomeratesByField2 = new HashSet<Conglomerate>();
    }

    [Key]
    public int ID_Store { get; set; }
    public string StoreName { get; set; }

    [ForeignKey("ID_Store1")]
    public virtual ICollection<Conglomerate> ConglomeratesByField1 { get; set; }
    [ForeignKey("ID_Store2")]
    public virtual ICollection<Conglomerate> ConglomeratesByField2 { get; set; }
} 
公共部分类存储
{
公共商店()
{
this.sbyField1=新HashSet();
this.congregatesbyfield2=新的HashSet();
}
[关键]
公共int ID_存储{get;set;}
公共字符串StoreName{get;set;}
[ForeignKey(“ID_Store1”)]
公共虚拟ICollection联合体ByField1{get;set;}
[ForeignKey(“ID_Store2”)]
公共虚拟ICollection聚合sbyField2{get;set;}
} 

对于代码优先,是否可以有一个“congregates”属性,即congregatesbyfield1和congregatesbyfield2的“并集”,或者我必须用专门的方法扩展该类?

因为您有两个不同的关系,所以需要两个单独的导航属性。不能将单个导航属性映射为多个关系的端点

要获得组合集合,可以引入(只读且未映射)帮助器属性:

public IEnumerable<Conglomerate> ConglomeratesByField
{
    get { return ConglomeratesByField1.Union(ConglomeratesByField2); }
}
public IEnumerable聚合字段
{
获取{return congregatesbyfield1.Union(congregatesbyfield2);}
}