【问题标题】:.NET Core Update migration is trying to re-create table again.NET Core 更新迁移正在尝试再次重新创建表
【发布时间】:2020-03-06 09:28:11
【问题描述】:

我首先在编写 .net core 3 项目代码。在这一步中,我在表格中添加了 2 列。然后我通过此代码 (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,所有应用的迁移都必须保存在那里。如果不是,但已经应用(创建或更新的表),何时再次尝试应用它,将抛出异常
  • 当您创建模型并且您没有为第一次迁移应用更新时,该表或表已经存在。附带说明:迁移始终基于您的快照(在迁移文件夹中),而不是运行dotnet ef migrations add 命令时的实际数据库布局
  • 我被截断了表,所以它也截断了迁移表。我了解到 ef 正在寻找迁移表以供申请。

标签: .net entity-framework asp.net-core .net-core entity-framework-core


【解决方案1】:

我在我的数据库中看到了问题。由于截断了数据库中的“__EFMigrationsHistory”表。由于未应用迁移,系统的行为正在变为初始迁移。

【讨论】:

    猜你喜欢
    • 2021-06-28
    • 1970-01-01
    • 2020-07-12
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 2015-06-08
    • 2018-09-24
    相关资源
    最近更新 更多