【发布时间】: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