dotnet/efcore · error · OperationException

The migration '{name}' has already been applied to the datab

Error message

The 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 instead.

What it means

During `RemoveMigration`, EF checks `HistoryRepository.GetAppliedMigrations()` for the latest migration's id; if it is recorded as applied and `force` is `false`, it throws `OperationException(RevertMigration)`. EF refuses to silently delete a migration whose changes already live in the target database, because doing so would desync the migration files from `__EFMigrationsHistory`.

Source

Thrown at src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs:302

                    {
                        Dependencies.OperationReporter.WriteVerbose(ex.ToString());
                        Dependencies.OperationReporter.WriteWarning(
                            DesignStrings.ForceRemoveMigration(migration.GetId(), ex.Message));
                    }
                }

                if (applied)
                {
                    if (force)
                    {
                        Dependencies.Migrator.Migrate(
                            targetMigration: migrations.Count > 1
                                ? migrations[^2].GetId()
                                : Migration.InitialDatabase);
                    }
                    else
                    {
                        throw new OperationException(DesignStrings.RevertMigration(migration.GetId()));
                    }
                }

                var migrationFileName = migration.GetId() + codeGenerator.FileExtension;
                var migrationFile = TryGetProjectFile(projectDir, migrationFileName);
                if (migrationFile != null)
                {
                    Dependencies.OperationReporter.WriteInformation(DesignStrings.RemovingMigration(migration.GetId()));
                    if (!dryRun)
                    {
                        File.Delete(migrationFile);
                    }

                    files.MigrationFile = migrationFile;
                }
                else
                {
                    Dependencies.OperationReporter.WriteWarning(

View on GitHub (pinned to dbf9771522)

Solutions

  1. Revert the database first: `dotnet ef database update <PreviousMigrationId>`, then `dotnet ef migrations remove`.
  2. If you are sure, pass `--force` (`RemoveMigration(..., force: true)`) to have EF roll the DB back automatically before deleting.
  3. If other databases already applied it, leave it applied and instead add a new migration that reverses the changes.

Example fix

// before
dotnet ef migrations remove
// after
dotnet ef database update 20240101000000_InitialCreate && dotnet ef migrations remove
// or
dotnet ef migrations remove --force
Defensive patterns

Strategy: validation

Validate before calling

// Check applied migrations before removing the latest one.
var history = serviceProvider.GetRequiredService<Microsoft.EntityFrameworkCore.Migrations.IHistoryRepository>();
var applied = (await history.GetAppliedMigrationsAsync())
    .Any(h => h.MigrationId.Equals(latestMigrationId, StringComparison.OrdinalIgnoreCase));

if (applied)
{
    // Either revert the DB first, or pass force: true, or add a reversing migration.
    throw new InvalidOperationException($"Migration '{latestMigrationId}' is applied; revert it or use --force.");
}

scaffolder.RemoveMigration(projectDir, rootNamespace, force: false);

Prevention

When it happens

Trigger: `dotnet ef migrations remove` (no `--force`) or `RemoveMigration(projectDir, rootNamespace, force: false, ...)` when the latest migration row exists in `__EFMigrationsHistory` of the configured database.

Common situations: Trying to undo a migration that was already deployed to a dev/shared/prod database, multiple developers sharing one database, or CI that applied migrations before you tried to remove them.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/8da7cedc9f3b587a. Report an issue: GitHub.