我想将 ASP.Net core 2.2 中模型的主键的数据类型从 int 更改为 long,因为它即将超出 int 范围。该应用程序以代码为先,并使用 SQL 服务器作为数据库。

更改模型中的数据类型有效并且运行 Add-Migration 也有效,但 Update-Database 失败并显示错误:

System.Data.SqlClient.SqlException (0x80131904): The index 'IX_tblMTransactions_OriginalTransactionId' is dependent on column 'OriginalTransactionId'.
The object 'FK_tblMTransactions_tblMTransactions_OriginalTransactionId' is dependent on column 'OriginalTransactionId'.

所以我读了它,似乎需要对迁移进行一些调整,例如在列更改之前删除外键和索引,然后将它们带回来。不幸的是,我在 .net core 2.2 中找不到任何有关执行此操作的有用信息,我找到的最接近的是 this question 但它讨论了 .net core 2.1 以及选项 DropForeignKeyDropIndex提到根据 this article ,.net core 2.2 似乎不再可用。

这是迁移类:
public partial class _20190817_1746 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<long>(
            name: "OriginalTransactionId",
            table: "tblMTransactions",
            nullable: true,
            oldClrType: typeof(int),
            oldNullable: true);

        migrationBuilder.AlterColumn<long>(
            name: "Id",
            table: "tblMTransactions",
            nullable: false,
            oldClrType: typeof(int))
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<int>(
            name: "OriginalTransactionId",
            table: "tblMTransactions",
            nullable: true,
            oldClrType: typeof(long),
            oldNullable: true);

        migrationBuilder.AlterColumn<int>(
            name: "Id",
            table: "tblMTransactions",
            nullable: false,
            oldClrType: typeof(long))
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
    }
}

这是该表的列、键和索引的屏幕截图:
c# - 将模型 .net Core 2.2 中关键属性的数据类型从 int 更改为 long-LMLPHP

有人知道如何调整 .NET Core 2.2 中的迁移类以使其工作吗?

谢谢

最佳答案

感谢 Ivan Stoev 我能够找到解决方案。我必须像这样手动添加索引和键的删除和创建:

public partial class _20190817_2317 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropForeignKey(
            name: "FK_tblMTransactions_tblMTransactions_OriginalTransactionId",
            table: "tblMTransactions");

        migrationBuilder.DropIndex(
            name: "IX_tblMTransactions_OriginalTransactionId",
            table: "tblMTransactions");

        migrationBuilder.DropPrimaryKey(
            name: "PK_tblMTransactions",
            table: "tblMTransactions");

        migrationBuilder.AlterColumn<long>(
            name: "OriginalTransactionId",
            table: "tblMTransactions",
            nullable: true,
            oldClrType: typeof(int),
            oldNullable: true);

        migrationBuilder.AlterColumn<long>(
            name: "Id",
            table: "tblMTransactions",
            nullable: false,
            oldClrType: typeof(int))
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

        migrationBuilder.AddPrimaryKey(
            name: "PK_tblMTransactions",
            table: "tblMTransactions",
            column: "Id");

        migrationBuilder.CreateIndex(
            name: "IX_tblMTransactions_OriginalTransactionId",
            table: "tblMTransactions",
            column: "OriginalTransactionId");

        migrationBuilder.AddForeignKey(
            name: "FK_tblMTransactions_tblMTransactions_OriginalTransactionId",
            table: "tblMTransactions",
            column: "OriginalTransactionId",
            principalTable: "tblMTransactions",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<int>(
            name: "OriginalTransactionId",
            table: "tblMTransactions",
            nullable: true,
            oldClrType: typeof(long),
            oldNullable: true);

        migrationBuilder.AlterColumn<int>(
            name: "Id",
            table: "tblMTransactions",
            nullable: false,
            oldClrType: typeof(long))
            .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
            .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
    }
}

再次感谢,伊万!!

关于c# - 将模型 .net Core 2.2 中关键属性的数据类型从 int 更改为 long,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57538018/

10-16 12:13