【发布时间】:2017-06-13 15:53:43
【问题描述】:
它is documented 并知道 EF 核心迁移脚本不支持删除列。所以我试着用手来做。
我的模型类是:
class Master
{
public int Id { get; set; }
public string ToBeDeleted { get; set; }
}
class Detail
{
public int Id { get; set; }
public Master Master { get; set; }
}
我的背景:
class Context : DbContext
{
public DbSet<Master> Masters { get; set; }
public DbSet<Detail> Details { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=local.db");
base.OnConfiguring(optionsBuilder);
}
}
我创建了一个迁移脚本,然后运行以下程序来创建 db 文件并添加几行:
class Program
{
static void Main(string[] args)
{
using (var context = new Context())
{
context.Database.Migrate();
if(!context.Masters.Any())
{
var master = new Master {ToBeDeleted = "Some string"};
context.Add(master);
context.Add(new Detail {Master = master});
context.SaveChanges();
}
}
}
}
我删除了Master 类的ToBeDeleted 属性并生成了第二个迁移脚本,该脚本生成的非常简单的代码无法正常工作,因为它只会在未来得到支持:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ToBeDeleted",
table: "Masters");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "ToBeDeleted",
table: "Masters",
nullable: true);
}
所以是时候写我自己的东西了,这就是我尝试过的:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql("PRAGMA foreign_keys=OFF");
migrationBuilder.CreateTable(
name: "NEW_Masters",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
},
constraints: table =>
{
table.PrimaryKey("PK_Masters", x => x.Id);
});
migrationBuilder.Sql("INSERT INTO NEW_Masters SELECT Id FROM Masters;");
migrationBuilder.DropTable("Masters");
migrationBuilder.RenameTable("NEW_Masters", newName: "Masters");
migrationBuilder.Sql("PRAGMA foreign_keys=OFF");
}
但是这会导致context.Database.Migrate() 抛出异常:
“Microsoft.Data.Sqlite.SqliteException”类型的未处理异常 发生在 Microsoft.EntityFrameworkCore.Relational.dll 中
附加信息:SQLite 错误 19:'FOREIGN KEY 约束 失败”。
最后一个问题:如何在迁移脚本中手动删除一列?
更新
根据我在讨论中得到的建议,我使用 Script-Migration 从迁移脚本生成 sql 并得到了这个:
CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
"MigrationId" TEXT NOT NULL CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY,
"ProductVersion" TEXT NOT NULL
);
CREATE TABLE "Masters" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_Masters" PRIMARY KEY AUTOINCREMENT,
"ToBeDeleted" TEXT
);
CREATE TABLE "Details" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_Details" PRIMARY KEY AUTOINCREMENT,
"MasterId" INTEGER,
CONSTRAINT "FK_Details_Masters_MasterId" FOREIGN KEY ("MasterId") REFERENCES "Masters" ("Id") ON DELETE RESTRICT
);
CREATE INDEX "IX_Details_MasterId" ON "Details" ("MasterId");
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20170127204056_Migration1', '1.1.0-rtm-22752');
INSERT INTO Masters (ToBeDeleted) VALUES ("ASDF"); --I've added this line manually for test only
INSERT INTO Details (MasterId) VALUES (1); --I've added this line manually for test only
PRAGMA foreign_keys="0";
CREATE TABLE "NEW_Masters" (
"Id" INTEGER NOT NULL CONSTRAINT "PK_Masters" PRIMARY KEY AUTOINCREMENT
);
INSERT INTO NEW_Masters SELECT Id FROM Masters;;
DROP TABLE "Masters";
ALTER TABLE "NEW_Masters" RENAME TO "Masters";
PRAGMA foreign_keys="1";
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20170127204851_Migration2', '1.1.0-rtm-22752');
脚本运行良好。例外是 EF 在某处执行的一些外键检查。
【问题讨论】:
-
哪一行抛出异常?
-
@CL.
context.Database.Migrate()
标签: sql sqlite entity-framework-core