Change data type from int to long of key property in model .net Core 2.2

4

I want to change the data type of the primary key of a model in ASP.Net core 2.2 from int to long since it is about to run out of the int range. The application was done code-first and uses SQL server as database.

Changing the data type in the model works and running Add-Migration works too, but Update-Database fails with the error:

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'.

So I read about it and it seems that there are some adjustments that need to be done to the migration like dropping foreign keys and indexes prior to the column change and bringing them back afterwards. Unfortunately I couldn't find any helpful information about doing this in .net core 2.2, the closest one I found was this question but it talks about .net core 2.1 and the option DropForeignKey and DropIndex mentioned there don't seem to be available any longer on .net core 2.2 according to this article.

Here is the migration class:

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);
    }
}

Here is a screenshot of the columns, keys and indexes of that table: Table

Does anybody know how to adjust the migration class in .NET Core 2.2 in order to make that work?

Thanks

c#
asp.net-core-mvc
entity-framework-core-2.2
asked on Stack Overflow Aug 17, 2019 by josibu

1 Answer

1

Thanks to Ivan Stoev I was able to find the solution. I had to add the dropping and Creating of the indexes and keys manually like this:

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);
    }
}

Thanks again, Ivan!!

answered on Stack Overflow Aug 17, 2019 by josibu

User contributions licensed under CC BY-SA 3.0