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:
Does anybody know how to adjust the migration class in .NET Core 2.2 in order to make that work?
Thanks
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:
Thanks again, Ivan!!