我使用EntityFrameworkCore.Sqlite
,我想使用迁移一切正常,但当我想删除列时,我无法更新数据库。我知道Sqlite
有局限性。
这是我处理这个问题的方法:
1.创建新表 2.将oldTable数据插入新表 3.Drop oldTable 4.将新表重新命名为oldtable
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(name: "XSettings",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(nullable: true),
LName = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Settings", x => x.Id);
});
migrationBuilder.Sql("ALTER TABLE Settings DROP COLUMN HName;");
migrationBuilder.Sql("INSERT INTO XSettings * FROM Settings;");
migrationBuilder.DropTable(name: "Settings");
migrationBuilder.Sql("ALTER TABLE XSettings RENAME TO Settings;");
}
但是当我运行我的应用程序时,HName列仍然存在,那么如何删除列?
要执行drop column操作,必须提供自SQLite限制以来的行SQL代码。 Microsoft Docs: SQLite Limitations
migrationBuilder.Sql("...");
语句中,或者可以将其拆分为多个语句只需在复制到C#时注意