EF Core Remove Migration

Removing a migration in EF Core is the process of deleting files of the most recent unapplied migration created. To remove those migration files, a Remove Migration command must be run. This command checks the database for all applied migrations and deletes the most recent migration files only if they haven't been applied to the database yet.

There are 2 ways to run the Remove Migration command and both will be covered in this article:

  • Remove-Migration: When using the Package Manager Console (PMC)
  • dotnet ef migrations remove: When using the .NET Command Line Interface (.NET CLI)
EF Core Remove Migration

The Remove Migration command is one of the 3 main migration commands:

How to use the Remove-Migration command with PMC in EF Core?

To use the Remove-Migration command with the Package Manager Console (PMC) in EF Core:

  1. Launch the Package Manager Console within Visual Studio.
  2. Ensure the default project in the console is the one containing your EF Core DbContext.
  3. Enter the following command:
|
Remove-Migration

NOTE: If you want to remove multiple migrations, you will need to run the command multiple times.

See Package Manager Console Parameters for additional parameters options.

Remove Migration - PMC Example

How to use the Remove Migration command with .NET CLI in EF Core?

To use the dotnet ef migrations remove command with the .NET Command Line Interface (.NET CLI) in EF Core:

  1. Open a command prompt or terminal.
  2. Navigate to the directory containing your context.
  3. Run the following command:
|
dotnet ef migrations remove

NOTE: If you want to remove multiple migrations, you will need to run the command multiple times.

See Command Line Parameters for additional parameters options.

Remove Migration - CLI Example

What does the Remove-Migration command accomplish in EF Core?

When you use the Remove-Migration command with PMC or dotnet ef migrations remove with .NET CLI, both utilize EF Core Tools to perform several tasks:

  1. First, EF Core Tools fetch all migrations that have been applied to your database.
  2. Then, EF Core Tools check if the latest migration has been applied.
    1. An error will be thrown if that's the case.
  3. The EF Core Tools then remove the latest unapplied migration by:
    1. Removing the corresponding migration files from your project.
    2. Updating the Model Snapshot to reflect the state before the latest migration.

What are the parameters for the Remove Migration command in EF Core?

Package Manager Console (PMC)

The Remove-Migration command with PMC offers several optional options:

Option Description
-Force When used, the Remove-Migration -Force command will remove the migration regardless of whether it has already been applied to the database.
-Context The context class to use that contains the migration you want to remove, it can be either the name or the fullname including namespaces. For example, Remove-Migration -Context Z.MyContextName will remove the migration for the Z.MyContextName context.
-Project The project containing the context used to remove the migration file. If omitted, the default project in the Package Manager Console is used. For example, Remove-Migration -Project Z.MyProjectName will revove the migration for the context in the Z.MyProjectName project.
-StartupProject The project with the configurations and settings. If omitted, the startup project of your solution is used. For example, Remove-Migration -StartupProject Z.MyStartupProjectName will remove the migration using the configurations and settings of the Z.MyStartupProjectName project.
language-csharp
|
Remove-Migration -Force -Context Z.MyContextName -Project Z.MyProjectName -StartupProject Z.MyStartupProjectName

NOTE: The -Project option specifies which project contains the context, while the -StartupProject option indicates the project that has the application's configurations and settings.

.NET Command Line Interface (.NET CLI)

For those working outside of Visual Studio or preferring the command line, EF Core offers similar functionality but with a slightly different syntax:

Option Short Description
--force -f Forces the removal of the migration even if it has already been applied to the database.
--context -c Specifies which DbContext to use.
--project -p The project containing the migrations to be removed.
--startup-project -s The project with the required configurations and settings.
language-csharp
|
dotnet ef database update [TargetMigration] -c MyContext -p MigrationsProject -s StartupProject

NOTE: Refer to the documentation above about Package Manager Console Parameters for more examples.

Troubleshooting

How to fix in EF Core: The term 'Remove-Migration' isn't recognized as a valid command, function, script file, or operable program.
Exception Message:
PM> Remove-Migration
Remove-Migration : The term 'Remove-Migration' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ Remove-Migration
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Remove-Migration:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Exception - The migration has already been applied to the database
Cause:

This error arise when the Microsoft.EntityFrameworkCore.Tools package isn't referenced in your migrations project.

Solution:

Ensure the Microsoft.EntityFrameworkCore.Tools package is installed or referenced in your migrations project.

You can install it via NuGet Package Manager:

Install-Package Microsoft.EntityFrameworkCore.Tools

Or using .NET CLI:

dotnet add package Microsoft.EntityFrameworkCore.Tools

How to fix in EF Core: The migration '[MigrationName]' has already been applied to the database. Revert it and try again.
Exception Message:
PM> Remove-Migration
The migration 'AddingEFExtensions' has already been applied to the database. Revert it and try again.
Exception - The migration has already been applied to the database
Cause:

This error arises when attempting to remove a migration that has already been committed to the database.

Solution:

Before using Remove Migration, make sure to revert the database to the state prior to the migration you intend to remove. This can typically be achieved using the Update-Database command followed by the name of the migration preceding the one you wish to remove.



Date Modified: 2023-09-19
Author:

Edit this page in GitHub

Got any EF Core Question?