【问题标题】:AspNetCore 2.0.5 Add-Migration trying to create table instead of alter existingAspNetCore 2.0.5 Add-Migration 尝试创建表而不是更改现有表
【发布时间】:2018-03-27 20:44:15
【问题描述】:

AspNetCore.All 2.0.5 决定为迁移创建一个与现有表同名的新表,而不是更改现有表。

Add-Migration 成功生成迁移,但在运行Update-Database 时会抛出错误,因为同名表已经存在

我可以在开发阶段删除表格或手动修复脚本,但从长远来看这对我没有帮助。为了将来的迁移,我需要了解这里发生了什么。

这是应用的初始迁移。 DB 只有一张表。

using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;

namespace GameApplication.Migrations
{
    public partial class first : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "UserProfiles",
                columns: table => new
                {
                    Id = table.Column<long>(nullable: false)
                        .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
                    Name = table.Column<string>(nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_UserProfiles", x => x.Id);
                });
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "UserProfiles");
        }
    }
}

然后在新的添加迁移之后,它包括以下内容:

    migrationBuilder.CreateTable(
        name: "UserProfiles",
        columns: table => new
        {
            Id = table.Column<long>(nullable: false)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
            City = table.Column<string>(nullable: true),
            Country = table.Column<string>(nullable: true),
            DisplayName = table.Column<string>(nullable: true),
            FirstName = table.Column<string>(nullable: true),
            Langauge = table.Column<float>(nullable: false),
            LastName = table.Column<string>(nullable: true),
            Latitude = table.Column<float>(nullable: false),
            Longitude = table.Column<float>(nullable: false),
            UserId = table.Column<string>(nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_UserProfiles", x => x.Id);
        });
  • 就像它完全忽略了之前的迁移存在;没有提到Name 被删除并替换为FirstName LastName 的事实,这通常会显示数据丢失警告。什么会导致这种情况发生?

看似相关的未回答问题: Add-Migration not adding column to existing table in Entity Framework Core

【问题讨论】:

    标签: asp.net-core-2.0 entity-framework-migrations ef-core-2.0


    【解决方案1】:

    我在写问题时解决了这个问题,所以:

    EF 弄清楚如何根据 ApplicationContextModelSnapshot.cs 创建新的迁移,而这个文件在我的情况下是不正确的。

    此快照文件对我来说已损坏,因为我的 20180313134743_first.Designer.cs 文件已被修改和评论。这意味着 EF 无法正确创建快照或快照不存在,使其在尝试进行新迁移时不知道当前模型状态。

    解决它:

    1. 我使用remove-migration 命令放弃了新的迁移。
    2. 手动修复快照 - 将 20180313134743_first.Designer.cs BuildTargetModel() 的内容复制到 ApplicationContextModelSnapshot.cs BuildModel()
    3. 运行Update-Database 进行验证,然后运行Add-Migration,这一次它成功识别出需要更改现有表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多