2017-09-03 84 views
1

我有一个多一对多的关系模型:EF核心OnDelete限制增加了额外的列

用户

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

    [Required] 
    [StringLength(100)] 
    public string Username { get; set; } 

    [Required] 
    public string PasswordHash { get; set; } 

    [ForeignKey("CompanyId")] 
    public Company Company { get; set; } 

    public ICollection<UserRole> UserRoles { get; set; } 
} 

角色

public class Role 
{ 
    public int Id { get; set; } 
    public int CompanyId { get; set; } 

    [Required] 
    [StringLength(100)] 
    public string Name { get; set; } 

    [StringLength(500)] 
    public string Description { get; set; } 

    [ForeignKey("CompanyId")] 
    public Company Company { get; set; } 

    public ICollection<RolePrivilege> RolePrivileges { get; set; } 
} 

UserRole的

public class UserRole 
{ 
    public int Id { get; set; } 
    public int UserId { get; set; } 
    public int RoleId { get; set; } 

    [ForeignKey("UserId")] 
    public User User { get; set; } 

    [ForeignKey("RoleId")] 
    public Role Role { get; set; } 
} 

当我创建迁移,然后三ed更新数据库,它抛出了多个级联路径的错误。该解决方案是就删除,无操作,所以我在OnModelCreating添加了这个:

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<UserRole>() 
      .HasIndex(e => new { e.UserId, e.RoleId }) 
      .IsUnique(); 

    modelBuilder.Entity<UserRole>() 
     .HasOne(e => e.User) 
     .WithMany() 
     .OnDelete(DeleteBehavior.Restrict); 

    modelBuilder.Entity<UserRole>().ToTable("UserRoles"); 
} 

现在正在创建的表,但有一两件事,我没有预料到的是它使一个额外的列。迁移代码如下所示:

migrationBuilder.CreateTable(
      name: "UserRoles", 
      columns: table => new 
      { 
       Id = table.Column<int>(type: "int", nullable: false) 
        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn), 
       RoleId = table.Column<int>(type: "int", nullable: false), 
       UserId = table.Column<int>(type: "int", nullable: false), 
       UserId1 = table.Column<int>(type: "int", nullable: true) 
      }, 
      constraints: table => 
      { 
       table.PrimaryKey("PK_UserRoles", x => x.Id); 
       table.ForeignKey(
        name: "FK_UserRoles_Roles_RoleId", 
        column: x => x.RoleId, 
        principalTable: "Roles", 
        principalColumn: "Id", 
        onDelete: ReferentialAction.Cascade); 
       table.ForeignKey(
        name: "FK_UserRoles_Users_UserId", 
        column: x => x.UserId, 
        principalTable: "Users", 
        principalColumn: "Id", 
        onDelete: ReferentialAction.Restrict); 
       table.ForeignKey(
        name: "FK_UserRoles_Users_UserId1", 
        column: x => x.UserId1, 
        principalTable: "Users", 
        principalColumn: "Id", 
        onDelete: ReferentialAction.Restrict); 
      }); 

正如您所看到的,它添加了一个额外的列UserId1。

我在做什么错,或者我该如何防止这种情况发生?

回答

2

这是典型关系流畅配置错误的结果 - 使用无参数过载Has/With(有效地告诉EF没有相应的导航属性),而实际上存在导航属性。在这种情况下,EF会将缺少的导航属性映射到另一个关系,而在另一端没有导航属性,并按照惯例的FK属性/列名称进行默​​认。

要解决该问题,请确保使用表示存在/不存在导航属性的正确重载(并根据添加/删除导航属性的情况更新它们)。在你的情况下,更换

.WithMany() 

.WithMany(e => e.UserRoles)