EF Core Migration Add - 使用意外的 DropForeignKey、DropUniqueConstraint、DropColumn 语句创建迁移

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

我对使用 EF Core 的应用程序进行了一些简单的更改。我只是更改了几个 Decimal 类型列的精度,仅此而已。当我尝试使用 dotnet ef migrations add ... 命令创建新迁移时,我看到了我希望看到的迁移语句:

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

一切看起来都很好,除了它还显示了大量奇怪的 DropForeignKeyDropUniqueConstraintDropColumn 语句。我经历了其中的一些, DropX 语句中引用的 none 列甚至存在于数据库或模型 类 中。这些 DropX 语句中的列名通常以 X_TempIdX_TempId1 等结尾。

我要求我的同事检查(使用 git)与我拥有的完全相同的分支和 运行 完全相同的命令(使用完全相同的 dotnet 版本),但是他的迁移没有显示任何奇怪的 DropXYZ 语句。如果我错了,请纠正我,但我相信 dotnet ef migrations add ... 命令将 SomethingModelSnapshot 的当前状态与模型 类 中的最新状态进行比较,并创建数据库更改以从当前状态获取状态为最新的。如果是这样,对同一个文件集的 运行ning dotnet ef migrations add ... 命令应该总是产生相同的结果,不是吗?

关于可能发生的事情的任何想法and/or如何解决这个问题?

一些版本:

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

我遇到了同样的问题。我也在使用 Core 2.2。能够通过进行新的初始迁移来解决它。之后的新迁移没有这个问题。

经过一番调查,这似乎是 EF Core 的一个错误。你可以在这里跟踪它 on their github.

这是从 2.1 升级到 2.2 后发生的。但请注意,这不仅仅是 EF Core 2.2 的错误,因为从头开始重新创建新迁移在 2.2 上运行得很好,但这更像是解释模型快照的重大变化。