We continue the theme of the Entity Framework commands! Commands that we have already seen:
Remove-Migration
As the name implies, this command is used to remove a migration, specifically the most recent one. It is useful in situations where you have created one or several migrations, and you discover that you have made an error, and you want to undo those migrations. An important consideration is whether the migration has already been applied or not in the database.
If the migration has not been applied to the database, in essence, there is no consideration to have, the command will be applied as you expect, that is, it will simply remove the files from the migrations that you indicate you want to delete.
If the migration has already been applied to the database, then removing a migration involves touching that database to revert the changes that were made to it. For example: If in a migration you added a column in a table, then when you remove that migration you will also want to remove that column. Sometimes this could cause data loss, so it is important to proceed with caution.
In the dotnet CLI, this command is found in:
dotnet ef migrations remove
These are some of the options of Remove-Migration:
- Force: Reverts a migration even if it has been applied to the database. By default, if you try to remove a migration which has already been applied to the database, an error is thrown at us, the only way we can avoid this error is using the Force option.
This command also has the basic options such as Context and Project to indicate the Data Context and the project to use, respectively.
Examples:
Basic use: The basic use would be to simply execute the command without passing any option. This removes the last migration, as long as it has not been previously applied in the database. In the Package Manager Console we can do this in the following way:
Remove-Migration
In the dotnet CLI, the equivalent command would be the following:
dotnet ef migrations remove
Note: If you have more than one Data Context, it is mandatory that you specify in the previous command the name of the Data Context to be used.
Removing the penultimate migration: Although we can not specify the migration that we want to eliminate, we can execute the Remove-Migration command several consecutive times, which, in effect, allows us to delete the migration we want, but one at a time. So, if we want to remove the penultimate migration, we should run the commands to remove migrations twice. In the Package Manager Console this would be:
Remove-Migration
Remove-Migration
And in the dotnet CLI:
dotnet ef migrations remove
dotnet ef migrations remove
Remove a migration that was already applied in the database: To remove a migration that has already been applied in the database, we must use the Force option. In the Package Manager Console this is done as follows:
Remove-Migration -Force
In the case of the dotnet CLI, this would be done as follows:
dotnet ef migrations remove –force
Note: the force in the dotnet CLI has a double dash
Executing this command in this way will not only remove the file from the migration of your directory, but it will go to the database to revert the changes that had been made with the migration. In order to know what actions will Remove-Migration take on your database, you should go to the migration file and inspect the Down function, since this is the one that represents the actions to execute when you try to revert a database migration.
Note: If the migration has already been applied to the database and you try to use Remove-Migration without using the Force option, you will receive an error like the following:
The migration ‘Migration_Name’ 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.