简体   繁体   English

EF6 代码首先是多对多,具有与其中一个实体上的集合相同类型的附加属性

[英]EF6 code first Many to Many with additional property of the same type as the collection on one of the entities

I know this issue has many similar questions here on SO, but I couldn't find one that's exactly like mine, but I might be wrong and there exists one.我知道这个问题在 SO 上有很多类似的问题,但我找不到一个和我一模一样的问题,但我可能错了,而且存在一个。 So here's my question:所以这是我的问题:
We have two entities (C# poco classes), Meeting and User , that each one of them contains a collection of the other entity.我们有两个实体(C# poco 类), MeetingUser ,每个实体都包含另一个实体的集合。 Code, shortened for brevity:代码,为简洁起见缩短:

public class Meeting
{
    public int ModeratorId { get; set; }
    public virtual User Moderator { get; set; }    // Property of type User

    public virtual ICollection<User> Participants { get; set; } // Collection of User
}

And here's the second one:这是第二个:

public class User
{
    public virtual ICollection<Meeting> Meetings { get; set; }
}  

Now, when adding a migration, one would expect that EF would create a linking table, something like MeetingUsers , which it actually creates if I omit the Moderator property.现在,在添加迁移时,人们会期望 EF 会创建一个链接表,类似于MeetingUsers ,如果我省略了Moderator属性,它实际上会创建它。 But when I add the Moderator property, that is the same type as the items in Participants , EF removes the linking table and adds a Meeting_Id column in the migration.但是,当我添加与Participants中的项目相同类型的Moderator属性时,EF 会删除链接表并在迁移中添加一个Meeting_Id列。 Here are the migrations:以下是迁移:
first migration, with only the collections on the two classes, without the additional Moderator property on Meeting :第一次迁移,在两个类中只有 collections,在Meeting上没有额外的Moderator属性:

public override void Up()
    {   
        CreateTable(
            "dbo.MeetingUsers",
            c => new
                {
                    Meeting_Id = c.String(nullable: false, maxLength: 128),
                    User_ID = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.Meeting_Id, t.User_ID })
            .ForeignKey("dbo.Meetings", t => t.Meeting_Id, cascadeDelete: true)
            .ForeignKey("dbo.Users", t => t.User_ID, cascadeDelete: true)
            .Index(t => t.Meeting_Id)
            .Index(t => t.User_ID);
        
    }  

You can see that EF creates the linking table just fine (I omitted the Meeting and User for brevity).您可以看到 EF 很好地创建了链接表(为简洁起见,我省略了会议用户)。
And here's the second migration EF adds after I add the Moderator property:这是我添加Moderator属性后 EF 添加的第二个迁移:

public override void Up()
    {
        DropForeignKey("dbo.MeetingUsers", "Meeting_Id", "dbo.Meetings");
        DropForeignKey("dbo.MeetingUsers", "User_ID", "dbo.Users");
        DropIndex("dbo.MeetingUsers", new[] { "Meeting_Id" });
        DropIndex("dbo.MeetingUsers", new[] { "User_ID" });
        AddColumn("dbo.Users", "Meeting_Id", c => c.String(maxLength: 128));
        AddColumn("dbo.Meetings", "ModeratorId", c => c.Int());
        AddColumn("dbo.Meetings", "User_ID", c => c.Int());
        CreateIndex("dbo.Users", "Meeting_Id");
        CreateIndex("dbo.Meetings", "ModeratorId");
        CreateIndex("dbo.Meetings", "User_ID");
        AddForeignKey("dbo.Meetings", "ModeratorId", "dbo.Users", "ID");
        AddForeignKey("dbo.Users", "Meeting_Id", "dbo.Meetings", "Id");
        AddForeignKey("dbo.Meetings", "User_ID", "dbo.Users", "ID");
        DropTable("dbo.MeetingUsers");
    }  

As you see EF removed the linking table and adds a column on the Users table, which is not what I want.如您所见,EF 删除了链接表并在用户表上添加了一列,这不是我想要的。 I want instead to keep the linking table and just add a new column ModeratorId to the Meetings table.相反,我想保留链接表,只需将新列ModeratorId添加到会议表中。
How do I achieve that?我该如何做到这一点? I might add that we use data annotations throughout and not the fluent api of EF.我可能会补充一点,我们始终使用数据注释,而不是 EF 的流畅 api。

Thanks,谢谢,
ashilon阿希隆

You can define manually link table and you are good to go.您可以手动定义链接表,您可以使用 go。

 public class Meeting
 {
    public int ModeratorId { get; set; }
    public virtual User Moderator { get; set; }    // Property of type User


}

And here's the second one:  

public class User
{
    .....
} 
public MeetingUser{

   public int UserId{get;set;}
   public virtual User User{get;set;}
   public int MeetingId {get;set;}
   public virtual Meeting Meeting {get;set;}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM