繁体   English   中英

EFCore回滚迁移错误:无效类型的列用作键列

[英]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()我猜这是在迁移的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: "");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM