【问题标题】:EF6 code first Many to Many with additional property of the same type as the collection on one of the entitiesEF6 代码首先是多对多,具有与其中一个实体上的集合相同类型的附加属性
【发布时间】:2020-10-21 14:54:12
【问题描述】:

我知道这个问题在 SO 上有很多类似的问题,但我找不到与我一模一样的问题,但我可能错了,并且存在一个。所以这是我的问题:
我们有两个实体(C# poco 类),MeetingUser,每个实体都包含一个集合的另一个实体。代码,为简洁起见:

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
}

这是第二个:

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

现在,在添加迁移时,人们会期望 EF 会创建一个链接表,类似于 MeetingUsers,如果我省略 ,它实际上会创建它版主 属性。但是,当我添加 Moderator 属性(与 Participants 中的项目相同的类型时,EF 会删除链接表并在迁移中添加 Meeting_Id 列。以下是迁移:
第一次迁移,只有两个类的集合,Meeting 上没有额外的 Moderator 属性: p>

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);
        
    }  

您可以看到 EF 很好地创建了链接表(我省略了 MeetingUser简洁)。
这是我添加 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");
    }  

如您所见,EF 删除了链接表并在 Users 表上添加了一列,这不是我想要的。我想保留链接表,只需向 Meetings 表添加一个新列 ModeratorId
我该如何做到这一点?我可能会补充一点,我们始终使用数据注释,而不是 EF 的 fluent api。

谢谢,
阿什隆

【问题讨论】:

    标签: c# entity-framework-6 ef-code-first entity-framework-migrations .net-4.6.1


    【解决方案1】:

    您可以手动定义链接表,一切顺利。

     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;}
    }
    

    【讨论】:

    • 好的,我明白了,我对此表示感谢,但我发现没有办法告诉 EF 我想做什么有点令人惊讶,所以不是自动解决方案,比如属性例如,我必须使用手动解决方案,并带有我自己的链接表。如果实在没有其他办法,我就按照你的方式去。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多