eddyP23
eddyP23

Reputation: 6825

EF Core Migration Add - creates a migration with unexpected DropForeignKey, DropUniqueConstraint, DropColumn statements

I made some simple changes on our app that uses EF Core. I just changed the precision of several Decimal typed columns and that is it. When I tried creating a new migration using dotnet ef migrations add ... command, I see migration statements I expect to see:

        migrationBuilder.AlterColumn<decimal>(
            name: "Value_Amount",
            schema: "Payment",
            table: "Withdrawals",
            type: "DECIMAL(18, 5)",
            nullable: false,
            oldClrType: typeof(decimal),
            oldType: "DECIMAL(15, 2)");

Everything looks good apart that it is also showing tons of weird DropForeignKey, DropUniqueConstraint, DropColumn statements. I went through quite a few of them and none of the columns referenced in the DropX statements even exist in the Database or model classes. The column names in these DropX statements often end in X_TempId, X_TempId1, etc..

I asked my colleague to checkout (using git) the exact same branch as I have and run the exact same commands (using exact same dotnet versions), but his migration is not showing any weird DropXYZ statements. Please correct me if I am wrong, but I believe that dotnet ef migrations add ... command compares the current state from the SomethingModelSnapshot with the latest state in the Model classes and creates DB changes to get from the current state to the latest one. If this is so, running dotnet ef migrations add ... command on the same fileset should always produce the same results, shouldn't it?

Any ideas on what could be happening and/or how to fix this?

Some versions:

dotnet --version = 2.2.100
dotnet ef --version = 2.2.0-rtm-35687
<TargetFramework>netcoreapp2.1</TargetFramework>
Windows 10, commands executed via Powershell.

Upvotes: 4

Views: 915

Answers (2)

eddyP23
eddyP23

Reputation: 6825

After some investigation, it seems that this is a bug with EF Core. You can track it here on their github.

This happened after upgrade from 2.1 to 2.2. But note, that this is not a bug of EF Core 2.2 simply, as recreating a new migration from scratch works beautifully on 2.2, but this is more of a breaking change of interpreting model snapshot.

Upvotes: 2

Markus
Markus

Reputation: 351

I had the same problem. I'm also using Core 2.2. Was able to get around it by doing a new initial migration. New migrations after that didn't have this problem.

Upvotes: 0

Related Questions