【问题标题】:Altering column with index on it in Entity Framework Core在 Entity Framework Core 中更改带有索引的列
【发布时间】:2016-11-18 00:45:37
【问题描述】:

版本

SQL 引擎:MS SQL。

EF Core 软件包版本:

"Microsoft.EntityFrameworkCore": "1.0.1",
"Microsoft.EntityFrameworkCore.SqlServer": "1.0.1",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final"

问题

我已经更改了现有的列,该列上有索引,之前创建了一些迁移:

// ... other changes
migrationBuilder.CreateIndex(
    name: "IX_Interactions_OrganisationToId",
    table: "Interactions",
    column: "OrganisationToId");

我已将其从可选更改为必需(不可为空)。 Entity Framework Core 生成了这样的迁移:

// ... other changes
migrationBuilder.AlterColumn<Guid>(
    name: "OrganisationToId",
    table: "Interactions",
    nullable: false);

但是你不能改变上面有索引的列。不幸的是,在将字段更改为不可为空的情况下,EF Core 不支持这一点。

重命名字段时 - 就像一个魅力,在重命名之前只是删除索引并在更改之后恢复它。


问题

有什么方法可以配置 EF Core,他将涵盖这种情况并生成正确的迁移?

【问题讨论】:

  • 这在 EFCore 1.1.0 中已修复。见问题#6102

标签: entity-framework entity-framework-core


【解决方案1】:

目前我已经修改了迁移并在更改列之前下拉了索引,并在操作后手动恢复。

它完成了这项工作。

// drop index before altering column
migrationBuilder.DropIndex(name: "IX_Interactions_OrganisationToId", table: "Interactions");

// actually altering column
migrationBuilder.AlterColumn<Guid>(
   name: "OrganisationToId",
   table: "Interactions",
   nullable: false);

// restore index
migrationBuilder.CreateIndex(
   name: "IX_Interactions_OrganisationToId",
   table: "Interactions",
   column: "OrganisationToId");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 2021-12-22
    • 2017-04-21
    • 1970-01-01
    • 2021-04-17
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多