EFCore roll back migration error: column of invalid type to be used as key column

EFCore roll back migration error: column of invalid type to be used as key column

我 运行 遇到一个问题,我试图在已应用迁移的数据库上回滚迁移。

这是我得到的错误:

Failed executing DbCommand (8ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE [EventStaffRequest] ADD CONSTRAINT [PK_EventStaffRequest] PRIMARY KEY ([Id]);
System.Data.SqlClient.SqlException (0x80131904): Column 'Id' in table 'EventStaffRequest' is of a type that is invalid for use as a key column in an index.
Could not create constraint or index. See previous errors.

ClientConnectionId:29574816-2b1a-4490-a216-a54cd7a2d33b
Error Number:1919,State:1,Class:16
Column 'Id' in table 'EventStaffRequest' is of a type that is invalid for use as a key column in an index.
Could not create constraint or index. See previous errors.

这是我要回滚的迁移:

public partial class AddedCompositeKeyToEventStaffRequest : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropPrimaryKey(
            name: "PK_EventStaffRequest",
            table: "EventStaffRequest");

        migrationBuilder.DropIndex(
            name: "IX_EventStaffRequest_EventId",
            table: "EventStaffRequest");

        migrationBuilder.DropColumn(
            name: "Id",
            table: "EventStaffRequest");

        migrationBuilder.AddPrimaryKey(
            name: "PK_EventStaffRequest",
            table: "EventStaffRequest",
            columns: new[] { "EventId", "QualityTypeId" });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropPrimaryKey(
            name: "PK_EventStaffRequest",
            table: "EventStaffRequest");

        migrationBuilder.AddColumn<string>(
            name: "Id",
            table: "EventStaffRequest",
            nullable: false,
            defaultValue: "");

        migrationBuilder.AddPrimaryKey(
            name: "PK_EventStaffRequest",
            table: "EventStaffRequest",
            column: "Id");

        migrationBuilder.CreateIndex(
            name: "IX_EventStaffRequest_EventId",
            table: "EventStaffRequest",
            column: "EventId");
    }
}

如果相关,这是我的模型代码:

public class EventStaffRequest
{
    [Required]
    public string EventId { get; set; }
    public virtual Event Event { get; set; }

    [Required]
    public string QualityTypeId { get; set; }
    public virtual QualityType QualityType { get; set; }

    [Required]
    public int AmountRequired { get; set; }

    [Required]
    public int MinimumRating { get; set; }
}

创建此迁移是因为我决定需要将主键更改为复合键。我在我的 DbContext 中应用了一个复合主键(我猜这是你在迁移的 Up() 中看到的):

builder.Entity<EventStaffRequest>()
    .HasKey(esr => new { esr.EventId, esr.QualityTypeId });

为什么我回滚不成功?我不明白为什么 string 不是键索引的合适类型(我使用 GUID 作为键)。

迁移系统似乎有问题。问题不在于string(当然可以用来PK),而是maxLength。默认情况下,string 列的长度不受限制,但 PK 需要应用一些限制。

通常当你使用 string 列作为 PK 时,即使你没有指定 maxLength,EF 也会自动应用一些限制(据我所知,至少对于 SqlServer 是 450).有趣的是,以相反的顺序执行相同的操作会生成类似的迁移,其中交换了 UpDown 内容,并且 AddColumn 的代码完全相同。但在 Down 方法中执行时不会,因此该路径中一定存在差异(因此存在问题)。您可能会考虑将其发布在 EF Core 问题跟踪器中,以便他们知道(并最终修复它)。

无论如何,解决方案是显式添加 maxLength 参数到 AddColumn 调用:

migrationBuilder.AddColumn<string>(
    name: "Id",
    table: "EventStaffRequest",
    maxLength: 450,
    nullable: false,
    defaultValue: "");