EF代码优先6:多对多自我引用与其他字段的关系

时间:2014-03-12 17:25:44

标签: ef-code-first code-first ef-migrations

我正在使用EF6和Code First类,如下所示:

User.cs

public class User
{
    public int Id { get; set; }

    public string Name { get; set; }

    public bool IsActive { get; set; }

    public virtual ICollection<UserRelationship> UserRelationships { get; set; }
}

UserRelationship.cs

public class UserRelationship
{
    [Key, Column(Order = 0)]
    [ForeignKey("UserA")]
    public int UserAId { get; set; }

    [Key, Column(Order = 1)]
    [ForeignKey("UserB")]
    public int UserBId { get; set; }

    public virtual User UserA { get; set; }
    public virtual User UserB { get; set; }

    public DateTime CreatedOn { get; set; }

    [Required]
    public int CreatedByUserId { get; set; }

    public DateTime? UpdatedOn { get; set; }

    public int? UpdatedByUserId { get; set; }
}

当我生成迁移以创建无法识别其生成方式的字段时:

CreateTable(
            "dbo.UserRelationship",
            c => new
                {
                    UserAId = c.Int(nullable: false),
                    UserBId = c.Int(nullable: false),
                    CreatedOn = c.DateTime(nullable: false),
                    CreatedByUserId = c.Int(nullable: false),
                    UpdatedOn = c.DateTime(),
                    UpdatedByUserId = c.Int(),
                    **UserProfile_Id = c.Int(),**
                })
            .PrimaryKey(t => new { t.UserAId, t.UserBId })
            .ForeignKey("dbo.User", t => t.UserAId)
            .ForeignKey("dbo.User", t => t.UserBId)
            **.ForeignKey("dbo.User", t => t.User_Id)**
            .Index(t => t.UserAId)
            .Index(t => t.UserBId)
            **.Index(t => t.User_Id);**

如何做到这一点不是生成表中的User_Id(UserRelationship)列?

由于

1 个答案:

答案 0 :(得分:0)

试试这段代码:

public class User
{
public int Id { get; set; }

public string Name { get; set; }

public bool IsActive { get; set; }

[InverseProperty("UserA")]
public virtual ICollection<UserRelationship> UserARelationships { get; set; }

[InverseProperty("UserB")]
public virtual ICollection<UserRelationship> UserBRelationships { get; set; }
}


    public class UserRelationship
    {
        [Key, Column(Order = 0)]            
        public int UserAId { get; set; }

        [Key, Column(Order = 1)]
        public int UserBId { get; set; }

        [ForeignKey("UserAId")]
        [InverseProperty("UserARelationships")]
        public virtual User UserA { get; set; }

        [ForeignKey("UserBId")]
        [InverseProperty("UserBRelationships")]
        public virtual User UserB { get; set; }

        public DateTime CreatedOn { get; set; }

        [Required]
        public int CreatedByUserId { get; set; }

        public DateTime? UpdatedOn { get; set; }

        public int? UpdatedByUserId { get; set; }
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<UserRelationship>().HasRequired(c => c.UserA).WithMany(u => u.UserARelationships).HasForeignKey(c => c.UserAId).WillCascadeOnDelete(false);
        modelBuilder.Entity<UserRelationship>().HasRequired(c => c.UserB).WithMany(u => u.UserBRelationships).HasForeignKey(c => c.UserBId).WillCascadeOnDelete(false);

    }