它is documented並且已知EF核心遷移腳本不支持刪除列。所以我正在嘗試手動完成。EF Core - 刪除列解決方案(sqlite)
我的模型類:
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 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();
}
}
}
}
我刪除ToBeDeleted
Master
類別和基因的性質額定第二遷移腳本,它生成非常簡單的代碼,不工作,因爲它只會在未來得到支持:
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」 未處理的異常發生在百萬分之一oft.EntityFrameworkCore.Relational.dll
附加信息:SQLite錯誤19:'FOREIGN KEY約束 失敗'。
最後,問題:如何在移植腳本中手動刪除列?
UPDATE
以下建議我在討論了,我以前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()' –