【问题标题】:EF Core OnDelete restrict adds additional columnEF Core OnDelete 限制添加了额外的列
【发布时间】:2018-02-11 16:11:40
【问题描述】:

我有一个多对多关系的模型:

用户

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

用户角色

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

当我创建迁移然后尝试更新数据库时,它引发了多个级联路径的错误。对此的解决方案是在删除时,无操作,所以我在 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。

我做错了什么或如何防止这种情况发生?

【问题讨论】:

    标签: entity-framework asp.net-core entity-framework-core


    【解决方案1】:

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

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

    .WithMany()
    

    .WithMany(e => e.UserRoles)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-16
      • 2012-05-19
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 2020-07-14
      • 2020-02-25
      • 2018-03-14
      相关资源
      最近更新 更多