EF 4.1单个类中的多个多对多关系 [英] EF 4.1 Multiple Many to Many relationships in single class

查看:81
本文介绍了EF 4.1单个类中的多个多对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有多个多对多关系的类,它们映射到同一辅助类.我的EquipmentSet类具有两个Equipment对象数组,而Equipment类也具有数组EquipmentSet来确定设备属于哪个集合.

I have a class with multiple many to many relationships mapping to the same secondary class. My EquipmentSet class has two arrays of Equipment objects, and the Equipment class also has an array of EquipmentSets to determine which sets the equipment is a part of.

EF仅为第二个多对多"关系生成一个查找表.如何告诉EF为两者生成查找表?使用下面的代码时,仅生成表"ModelSpecificEquipment".永远不会生成"GlobalEquipment"表.

EF is only generating a lookup table for the second Many to Many relationship. How can I tell EF to generate lookup tables for both? When the code below is used, only the table "ModelSpecificEquipment" is generated. The table "GlobalEquipment" never gets generated.

public partial class EquipmentSet 
{
    public int Id { get; set; }
    public List<Equipment> Global { get; protected set; }
    public List<Equipment> ModelSpecific { get; protected set; }

    public EquipmentSet()
    {
        Global = new List<Equipment>();
        ModelSpecific = new List<Equipment>();
    }
}


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

    public Equipment()
    {
    }
}



public class DataContext : DbContext
{
    public DbSet<Equipment>  Equipment { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Equipment>()
            .HasMany<EquipmentSet>(x => x.EquipmentSets)
            .WithMany(x => x.Global)
            .Map(x =>
            {
                x.MapLeftKey("EquipmentId");
                x.MapRightKey("EquipmentSetId");
                x.ToTable("GlobalEquipment");
            });

        modelBuilder.Entity<Equipment>()
            .HasMany<EquipmentSet>(x => x.EquipmentSets)
            .WithMany(x => x.ModelSpecific)
            .Map(x =>
            {
                x.MapLeftKey("EquipmentId");
                x.MapRightKey("EquipmentSetId");
                x.ToTable("ModelSpecificEquipment");
            });
        base.OnModelCreating(modelBuilder);
    }
}

同样,在这一点上,EF创建的数据库仅包含3个表:EquipmentSets,Equipments,ModelSpecificEquipments. GlobalEquipments丢失.

Again, at this point the database that EF creates only contains 3 tables: EquipmentSets, Equipments, ModelSpecificEquipments. GlobalEquipments is missing.

推荐答案

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

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.

这篇关于EF 4.1单个类中的多个多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆