How do I remove most recent EFCore migration when SQLite provider cannot drop columns

时光毁灭记忆、已成空白 提交于 2021-02-10 14:28:38

问题


This is a simple EFCore database in a .NETCore 3.1 app that has 3 migrations

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

The most recent one is still checked out in working code. But after I made it and ran a few times, I realized I wanted to undo it, make more changes and apply it again. so I tried remove-migration from the package manager console. The error returned was

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.

A bit of searching here brought me to this thread which says that the solution is to update-database to the previous migration and then remove-migration. So I tried that but apparently there is an issue with the SQLite provider won't let me do it. The package manager console output is as follows:

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.

OK, so I can't take that approach. So what do I do? Is there some manual way I can fix this? I have the SQLite Compact Toolbox extension so I could edit the SQLite database directly if it can be done that way.

(I realize I might be able to just apply yet another migration on top of the one I've already made but I very much would like to avoid that scenario for unrelated reasons. Besides, this is an issue that might come up again so if there's a way to do this, I'd like to learn it)


回答1:


Answering my own question because I came across a solution that worked. Maybe this will help someone else. The link to the answer was actually in the very error text that I posted so I feel rightfully foolish for not having found it before posting.

In short, what I had to do was

  1. Open the database using the Sqlite Toolkit Extension
  2. Use that extension to generate a script of adding the problem table (with the column that needs dropping).
  3. Edit that script to just remove the bit that added the column I no longer wanted
  4. Rename that table to some other name.
  5. Run the script to re-generate the table as it was
  6. Drop the renamed version.

At this point I was able to update-database to the previous version and then remove-migration. Made the extra changes I wanted directly and then created a new migration with them. Seems to work well



来源:https://stackoverflow.com/questions/61683088/how-do-i-remove-most-recent-efcore-migration-when-sqlite-provider-cannot-drop-co

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!