asp.net-core-2.0 - AspNetCore 2.0.5 Add-Migration 尝试创建表而不是更改现有表

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

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

最佳答案

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

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,这次它成功地识别出它需要更改现有表。

关于asp.net-core-2.0 - AspNetCore 2.0.5 Add-Migration 尝试创建表而不是更改现有表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49513206/

相关文章:

c# - Entity Framework Core 1.1.2 迁移异常

entity-framework-5 - 如果添加一个继承自某些东西的类改变了代码的行为,是否违反了坚实的原则?

asp.net-core - EF核心2 : Database first with many-to-many (linking table)

c# - FK 约束可能会导致循环或多个级联路径

asp.net-core-2.0 - ASPNET Core OIDC 关联失败

.Net Core 2.0 - OpenID Connect - 无效的发现文档

c# - Entity Framework 5 迁移与数据迁移

c# - asp.net 核心身份来自 BaseApplicationUser 的多个用户

c# - 在 ActionFilter 中间件中使用 DbContext

c# - 使用 appsettings.Production.json 中的数组设置覆盖 appsettings.json 中的数组设置