2017-09-03 83 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)