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
- Revert the database first: `dotnet ef database update <PreviousMigrationId>`, then `dotnet ef migrations remove`.
- If you are sure, pass `--force` (`RemoveMigration(..., force: true)`) to have EF roll the DB back automatically before deleting.
- 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
- Run `dotnet ef migrations list` and confirm the target migration is unapplied before removing.
- For shared/production databases, prefer adding a new migration that reverses changes over removing an applied one.
- Use `--force` only when you intend EF to roll the database back automatically.
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
- You cannot add a migration with the name 'Migration'.
- The name '{migrationName}' is used by an existing migration.
- No ModelSnapshot was found.
- No DbContext named '{name}' was found.
- The wildcard '*' can only be used with commands that run for
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/8da7cedc9f3b587a.
Report an issue: GitHub.