且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

EF 4.1单个类中的多个多对多关系

更新时间:2023-02-15 18:03:37

我认为无法映射.您不能将关系的一侧的两个端点关联到关系的另一侧的一个端点.您可能需要这样的东西:

I think it is not possible to map this. You cannot relate two endpoints on one side to one single endpoint on the other side of a relationship. You will probably need something like this:

public partial class Equipment
{
    public int Id { get; set; }
    public List<EquipmentSet> GlobalEquipmentSets { get; set; }
    public List<EquipmentSet> ModelSpecificEquipmentSets { get; set; }

    public IEnumerable<EquipmentSet> EquipmentSets
    {
        get
        {
            return GlobalEquipmentSets.Concat(ModelSpecificEquipmentSets);
            // catch cases when one or both of the sets are null.
        }
    }
}

EquipmentSets在这里只是一个只读的帮助程序,未映射到数据库.

EquipmentSets is here only a readonly helper which isn't mapped to the database.

然后您可以在GlobalGlobalEquipmentSets之间创建多对多关系,并在ModelSpecificModelSpecificEquipmentSets之间创建另一个多对多关系.

You can then create a many-to-many relationship between Global and GlobalEquipmentSets and another many-to-many relationship between ModelSpecific and ModelSpecificEquipmentSets.