{"record":{"id":"8da7cedc9f3b587a","repo":"dotnet/efcore","slug":"the-migration-name-has-already-been-applied-to","errorCode":null,"errorMessage":"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.","messagePattern":"The migration '(.+?)' 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\\.","errorType":"exception","errorClass":"OperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs","lineNumber":302,"sourceCode":"                    {\n                        Dependencies.OperationReporter.WriteVerbose(ex.ToString());\n                        Dependencies.OperationReporter.WriteWarning(\n                            DesignStrings.ForceRemoveMigration(migration.GetId(), ex.Message));\n                    }\n                }\n\n                if (applied)\n                {\n                    if (force)\n                    {\n                        Dependencies.Migrator.Migrate(\n                            targetMigration: migrations.Count > 1\n                                ? migrations[^2].GetId()\n                                : Migration.InitialDatabase);\n                    }\n                    else\n                    {\n                        throw new OperationException(DesignStrings.RevertMigration(migration.GetId()));\n                    }\n                }\n\n                var migrationFileName = migration.GetId() + codeGenerator.FileExtension;\n                var migrationFile = TryGetProjectFile(projectDir, migrationFileName);\n                if (migrationFile != null)\n                {\n                    Dependencies.OperationReporter.WriteInformation(DesignStrings.RemovingMigration(migration.GetId()));\n                    if (!dryRun)\n                    {\n                        File.Delete(migrationFile);\n                    }\n\n                    files.MigrationFile = migrationFile;\n                }\n                else\n                {\n                    Dependencies.OperationReporter.WriteWarning(","sourceCodeStart":284,"sourceCodeEnd":320,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Migrations/Design/MigrationsScaffolder.cs#L284-L320","documentation":"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`.","triggerScenarios":"`dotnet ef migrations remove` (no `--force`) or `RemoveMigration(projectDir, rootNamespace, force: false, ...)` when the latest migration row exists in `__EFMigrationsHistory` of the configured database.","commonSituations":"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.","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."],"exampleFix":"// before\ndotnet ef migrations remove\n// after\ndotnet ef database update 20240101000000_InitialCreate && dotnet ef migrations remove\n// or\ndotnet ef migrations remove --force","handlingStrategy":"validation","validationCode":"// Check applied migrations before removing the latest one.\nvar history = serviceProvider.GetRequiredService<Microsoft.EntityFrameworkCore.Migrations.IHistoryRepository>();\nvar applied = (await history.GetAppliedMigrationsAsync())\n    .Any(h => h.MigrationId.Equals(latestMigrationId, StringComparison.OrdinalIgnoreCase));\n\nif (applied)\n{\n    // Either revert the DB first, or pass force: true, or add a reversing migration.\n    throw new InvalidOperationException($\"Migration '{latestMigrationId}' is applied; revert it or use --force.\");\n}\n\nscaffolder.RemoveMigration(projectDir, rootNamespace, force: false);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["ef-core","migrations","database","design-time"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}