【问题标题】:.NET Core 2.2 Migration Builder unable to remove index.NET Core 2.2 Migration Builder 无法删除索引
【发布时间】:2020-12-22 08:31:16
【问题描述】:

我正在尝试从 IdentityUserRole 表中名为 UserRole 的列中删除索引/外键。

UserRole 有 2 列。用户 ID 和角色 ID。两者都是主键。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);
    
    var rolesTable = modelBuilder.Entity<Role>().ToTable("Role");
    var userRolesTable = modelBuilder.Entity<UserRole>().ToTable("UserRole");
    var userClaimsTable = modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
    var usersTable = modelBuilder.Entity<User>().ToTable("User");

    modelBuilder.Entity<Role>(builder =>
    {
        builder.Metadata.RemoveIndex(new[] { builder.Property(u => u.NormalizedName).Metadata });
    });
    modelBuilder.Entity<UserRole>(builder =>
    {
        builder.Metadata.RemoveIndex(new[] { builder.Property(u => u.RoleId).Metadata});
    });
    

}

但是,每当我运行迁移并更新数据库时,仍会出现此索引:

列 RoleId 的 IX_UserRole_RoleId

编辑:这是生成的迁移

migrationBuilder.CreateTable(
    name: "UserRole",
    columns: table => new
    {
        UserId = table.Column<string>(nullable: false),
        RoleId = table.Column<string>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_UserRole", x => new { x.UserId, x.RoleId });
        table.ForeignKey(
            name: "FK_UserRole_Role_RoleId",
            column: x => x.RoleId,
            principalTable: "Role",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
        table.ForeignKey(
            name: "FK_UserRole_User_UserId",
            column: x => x.UserId,
            principalTable: "User",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

migrationBuilder.CreateIndex(
    name: "IX_UserRole_RoleId",
    table: "UserRole",
    column: "RoleId");

【问题讨论】:

  • 迁移是否会给您带来任何错误(是否存在包含阻止其正常运行的数据的外键链接?)。我的建议是备份数据库,然后为迁移生成 SQL,然后手动运行。您通常会从 DBMS 获得更多信息
  • 从我的包管理器控制台运行时没有错误。
  • 这是一个我正在执行迁移的新数据库。
  • 您能否发布由此更改生成的迁移?
  • 嗨@PaulMichaels,我已经用它编辑了我的问题。

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


【解决方案1】:

按照惯例,EF Core 为每个 FK 列添加索引,该列不是另一个索引的前导部分。即使您没有使用这些索引的查询,但通常它们对于强制执行 FK 约束以及实现级联删除很有用,因此事实上它们是关系数据库设计中的标准(类似于用于强制唯一索引的唯一索引/ PK 限制等)但这不是这里的问题。

您可能认为它是缺陷,但即使您使用显式RemoveIndex 元数据API,也无法删除约定引入的索引。它会删除它们,但随后会自动添加回来。

这是因为 EF Core 约定不是静态应用的。它们由侦听触发“重新应用”约定的不同模型元数据更改的类实现。

简而言之,您无法使用元数据/流式 API 删除此类索引。如果你真的想删除它们(我不建议这样做),你应该手动编辑生成的迁移并从 UpDown 方法中删除相应的创建/删除命令。

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 2020-05-22
    • 1970-01-01
    • 2012-02-11
    • 2019-11-21
    • 2016-11-05
    • 2019-07-01
    • 2020-04-29
    • 2019-06-01
    相关资源
    最近更新 更多