Many of us have been working on projects which use EF Core for data access. When the project is in its initial stages, generally there are a lot of modifications as the database design is supposed to be evolving.
When we work on database design modifications, we generate migrations and we apply them to the database. Sometimes, it may happen that you may see some errors in the applied migration. In that case, you may want to un-apply the applied migration. After “un-applying”, you may want to remove the generated EF Core migration files and you may want to generate new one.
In this article, we are going to have a look at how to “un-apply” and “remove” the EF core migration which has already been applied to the database.
A word of caution !
For each and every database modification, EF Core migrations are created and applied to the database.
Generally, if a migration is applied already to the database, and if you want to change anything from that migration, general guidance is to create another migration with intended modifications.
The question is then – when should we use the steps mentioned in this article ?
I follow a rule of thumb. If a migration is reviewed and merged already to the “main” branch, then it is better to create another migration to undo the modifications applied by that migration. If a migration is not yet merged to the “main” branch and if the change is still in development, then mostly, you may use the below steps to “un-apply” and “remove” a given migration.
I hope this clarifies on when the “un-apply” and “remove” migration steps should be used. Let’s move on !
Now, let’s write some code. We are going to create EF Core models, then we will generate migrations and then apply those to the database.
Remove Migration Error
Below code snippet shows the command to remove the migration.
If we try to remove this applied migration, we will get an error notifying that the migration is already applied to database and that’s why it cannot be removed.
In order to remove the applied migration, there are two options.
- One option – to delete the database and then remove the migration
- Another option is to unapply only the recent migration
Option 1 – Delete Database
As stated earlier, one of the option is delete the existing database. This will unapply all the migrations. Then we can remove the existing migrations by using remove command. Both commands are shown in the code snippet given below.
Option 2 – Unapply Recent Migration
In option 1, complete database is deleted. In this option, we are going to unapply only the recently applied migration. For unapplying recent migration, we need to apply the previous migration which was applied just before the latest migration.
We will need name of previous migration. This migration name can be found by query __EFMigrationsHistory table. In the result you will get latest migration as the last row. Get the migration id from second last row.
“Do not delete the migration entry directly from __MigrationHistory table. It may cause troubles as framework would think that the migration is not applied, but it is actually applied to the database, leaving database in an inconsistent state”
Then we can use database update to apply the previous migration. This will unapply the latest migration. Now, if we want, we can remove the migration by using the remove command. Both commands are given in the code snippet given below.
I hope you find this information helpful. Let me know your thoughts !