.NET Core 更新迁移尝试重新创建表格

4

我正在开发一个基于.NET Core 3的项目,采用代码优先模式。在这一步骤中,我向我的表中添加了两个列。然后,我通过以下代码(CLI)对解决方案进行了迁移操作。

dotnet ef --startup-project ../MyApi.Api migrations add actorInfomart

我的迁移返回了这个结果:

public partial class actorInfomart : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
        migrationBuilder.AddColumn<bool>(
            name: "IsSeries",
            schema: "film",
            table: "Movie",
            nullable: false,
            defaultValue: false);

        migrationBuilder.AddColumn<string>(
            name: "ExtInfo",
            schema: "act",
            table: "Actor",
            maxLength: 100,
            nullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "IsSeries",
            schema: "film",
            table: "Movie");

        migrationBuilder.DropColumn(
            name: "ExtInfo",
            schema: "act",
            table: "Actor");
    }
}

看起来是正确的。问题出在这里。当我尝试更新迁移时,就会出现以下错误:

There is already an object named 'Actor' in the database.

我的迁移正在尝试在我的数据库中创建新表。但是在迁移中没有为此做任何操作。以下是我的迁移更新CLI命令。

dotnet ef --startup-project ../MyApi.Api database update actorInfomart

在终端中(我正在使用Mac),输入以下命令:

info: Microsoft.EntityFrameworkCore.Migrations[20402]
      Applying migration '20191231100058_Initial'.
Applying migration '20191231100058_Initial'.

我该如何应用最新的迁移?在第一个和最后一个之间有10个迁移。这是我在迁移方面遇到的第一个问题。


在您的数据库中,有一个名为__migrations_History的表,所有已应用的迁移都必须保存在其中。如果没有保存,但已经应用(表已创建或更新),当尝试再次应用时,会抛出异常。 - AlleXyS
1
要么是在创建模型时表已经存在,而您没有为第一个迁移应用更新。另外需要注意的是:迁移始终基于快照(在迁移文件夹中),而不是在运行dotnet ef migrations add命令时的实际数据库布局。 - Tseng
我在截断表时也截断了迁移表。我学到了 EF 正在查找迁移表以进行应用。 - Ender Aric
1个回答

2
我在我的数据库中发现了问题。由于截断了DB中的“__EFMigrationsHistory”表,系统的行为变成了初始迁移,因为没有应用任何迁移。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接