【问题标题】:How do I remove most recent EFCore migration when SQLite provider cannot drop columns当 SQLite 提供程序无法删除列时,如何删除最近的 EFCore 迁移
【发布时间】:2020-08-24 05:49:17
【问题描述】:

这是一个 .NETCore 3.1 应用中的简单 EFCore 数据库,具有 3 个迁移

"init"      Initial migration
"gcalg500"  Added seeding data and tables
"valtt"     Added more seeding data and tables

最新的仍在工作代码中签出。但在我成功并运行了几次之后,我意识到我想撤消它,进行更多更改并再次应用它。所以我从包管理器控制台尝试了remove-migration。返回的错误是

The migration '20200507181032_valtt' has already been applied to the database. Revert it
and try again. If the migration has been applied to other databases, consider reverting its
changes using a new migration.

在这里搜索了一下,我找到了this thread,它说解决方案是update-database 到以前的迁移,然后然后 删除迁移。所以我尝试了,但显然 SQLite 提供程序存在问题,不允许我这样做。包管理器控制台输出如下:

PM> update-database gcalg500
Build started...
Build succeeded.
Reverting migration '20200507181032_valtt'.
System.NotSupportedException: SQLite does not support this migration operation ('DropColumnOperation'). For more information, see http://go.microsoft.com/fwlink/?LinkId=723262.
   at Microsoft.EntityFrameworkCore.Migrations.SqliteMigrationsSqlGenerator.Generate(DropColumnOperation operation, IModel model, MigrationCommandListBuilder builder, Boolean terminate)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.<>c.<.cctor>b__71_12(MigrationsSqlGenerator g, MigrationOperation o, IModel m, MigrationCommandListBuilder b)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model, MigrationCommandListBuilder builder)
   at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.SqliteMigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateDownSql(Migration migration, Migration previousMigration)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.<>c__DisplayClass15_1.<GetMigrationCommandLists>b__1()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
SQLite does not support this migration operation ('DropColumnOperation'). For more information, see http://go.microsoft.com/fwlink/?LinkId=723262.

好的,所以我不能采用这种方法。那我该怎么办?有什么手动方法可以解决这个问题吗?我有 SQLite Compact Toolbox 扩展,所以如果可以的话,我可以直接编辑 SQLite 数据库。

(我意识到我可能能够在我已经进行的迁移之上再应用一次迁移,但出于不相关的原因,我非常希望避免这种情况。此外,这可能会出现再次,所以如果有办法做到这一点,我想学习它)

【问题讨论】:

    标签: sqlite ef-core-3.1


    【解决方案1】:

    回答我自己的问题,因为我遇到了一个有效的解决方案。也许这会帮助别人。答案的链接实际上是在我发布的错误文本中,所以我觉得在发布之前没有找到它是非常愚蠢的。

    简而言之,我要做的是

    1. 使用 Sqlite Toolkit Extension 打开数据库
    2. 使用该扩展生成添加问题表的脚本(与列 需要丢弃)。
    3. 编辑该脚本以删除添加我不再需要的列的位
    4. 将该表重命名为其他名称。
    5. 运行脚本以原样重新生成表
    6. 删除重命名的版本。

    此时我能够将数据库更新到以前的版本,然后删除迁移。直接进行了我想要的额外更改,然后使用它们创建了新的迁移。看起来效果不错

    【讨论】:

      猜你喜欢
      • 2022-10-20
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      • 2016-08-07
      • 2023-01-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多