在Sqlite Migration中删除列

问题描述 投票:0回答:1

我使用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列仍然存在,那么如何删除列?

c# sqlite migration entity-framework-core ef-migrations
1个回答
0
投票

使用SQLiteStudio

要执行drop column操作,必须提供自SQLite限制以来的行SQL代码。 Microsoft Docs: SQLite Limitations

  1. SQLiteStudio站点下载SQLiteStudio
  2. 加载本地数据库文件
  3. 删除所需的列,并通过“提交结构已更改”(绿色确定按钮)显示SQL语句
  4. 复制生成的迁移代码,然后将其复制到migrationBuilder.Sql("...");语句中,或者可以将其拆分为多个语句

只需在复制到C#时注意

SQLiteStudio generated SQLite Statements

© www.soinside.com 2019 - 2024. All rights reserved.