In EF Core 10, what does dotnet ef migrations remove undo, and why does it refuse a migration already applied to the database?
answer
- latest migration only
- snapshot rebuilt from the previous Designer
- checks __EFMigrationsHistory first
- --force runs Down on that database
- shared database: add a corrective migration
basics
~20 sIt deletes the latest migration's two files and rebuilds the snapshot from the previous migration's metadata. If __EFMigrationsHistory shows the migration applied, it stops unless --force reverts it first; on shared databases you add a corrective migration instead.
solid answer
~40 s`dotnet ef migrations remove` works on the last migration only. It deletes `<id>_Name.cs` and `<id>_Name.Designer.cs` and regenerates the model snapshot from the previous migration's `BuildTargetModel`; your entity classes keep their changes, so you can adjust the model and add the migration again. Before deleting, it connects and checks `__EFMigrationsHistory`. If the migration is recorded there it throws, telling you to revert it first, because deleting the code would leave a database whose schema and history no code describes. `--force` reverts that database to the previous migration by running `Down`, then removes the files, which is fine for a disposable local database. Once a migration has reached staging or production, keep it and add a corrective migration.
code
bash · 8 lines# Not applied anywhere yet: just remove it
dotnet ef migrations remove
# Applied only to the local development database: revert it, then remove
dotnet ef migrations remove --force
# Check what the database has applied
dotnet ef migrations listgo deeper
Know that remove undoes only the last, unapplied migration and that your model changes stay in the entity classes.
Explain the history-table check, what --force does on the database, how the snapshot is rebuilt from the previous Designer file, and when a corrective migration replaces remove.
Show judgment once a migration has left your machine: never rewrite history other databases recorded, prefer forward fixes, and plan any revert while the migration code still exists.
Set the team rule for when a migration becomes immutable, how corrective migrations are reviewed, and how rollbacks are rehearsed against production-like data.
## What remove changes `dotnet ef migrations remove` is the undo for `dotnet ef migrations add` while a migration is still private to your machine. It always targets the **latest** migration in the migrations assembly and changes three things on disk: - It deletes the main migration file (`<id>_Name.cs`). - It deletes the metadata file (`<id>_Name.Designer.cs`). - It rewrites the **model snapshot** (`RentalDbContextModelSnapshot.cs`) from the previous migration's `BuildTargetModel`, or deletes the snapshot when the removed migration was the only one. It does **not** touch your entity classes or `OnModelCreating`. After a remove, the model still contains the change, so the usual loop is: remove, adjust the model, add the migration again. ## The applied check Before deleting anything, the command asks the database whether the migration ran: 1. It builds the projects and creates the `DbContext` at design time. 2. It reads the applied migration ids from `__EFMigrationsHistory`. 3. If the latest migration's id is there and `--force` was not passed, it throws: *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.* 4. If the id is there and `--force` was passed, it migrates that database back to the previous migration, which runs the migration's `Down`, and only then deletes the files. 5. If the id is not there, it deletes the files and rewrites the snapshot. The reason for the refusal is consistency. A database that applied a migration carries its schema change and a history row naming it. Delete the code and no migration describes that state any more: the next `migrations add` scaffolds the same change again, and applying it fails because the table or column already exists. ## When the database cannot be reached - Without `--force`, a connection failure stops the command, so nothing is deleted. - With `--force`, the command warns that it was *unable to check if the migration … has been applied* and removes the files anyway; if it had been applied, reverting it by hand is now your job. ## Choosing the right undo | Where the migration has gone | What to do | |---|---| | Nowhere: scaffolded, not applied | `dotnet ef migrations remove`, fix the model, add again | | Applied to your own disposable database | `dotnet ef migrations remove --force` (or update the database to the previous migration first, then remove) | | Applied to a shared, staging or production database | Keep it. Add a new migration that corrects the change | | Merged by teammates who already applied it | Keep it; their databases carry its history row too | Editing the `Up` of a migration that a shared database already ran is the same mistake in another form: those databases never run `Up` again, so only newly created databases see the edit and the schemas drift apart. `--force` is a shortcut for two steps you can also run yourself: update the local database to the previous migration (the target `0` when the migration being removed is the first one), then run `remove`. Either way the migration's `Down` executes, so a `Down` that drops a column also drops whatever test data you put in it. That is acceptable on a database you can recreate, and it is exactly why the same command is wrong for a database other people depend on. ## Things remove will not do - **Remove an older migration.** It has no argument naming a migration. To drop one from the middle of an unpublished sequence, remove the later ones in reverse order first, then remove it, then re-add the changes you keep. - **Clean up after a hand deletion silently.** If you deleted the last migration's files yourself, the snapshot no longer matches the last remaining migration; running `remove` notices that, says it assumes the migration was deleted manually, and rewrites the snapshot from the remaining migration's metadata. - **Undo a migration in production.** That is a deployment decision: a corrective migration, or a planned revert to an earlier migration while its code still exists. `dotnet ef migrations list` is the quick way to see where you stand: it connects and marks every migration the database has not applied as `(Pending)`, or skips the check with `--no-connect`.
- What does dotnet ef migrations remove do if the database cannot be reached?Without `--force` the connection error stops the command and nothing is deleted. With `--force` it prints a warning that it could not check whether the migration was applied, then removes the files and rewrites the snapshot; if the migration had in fact run somewhere, you must revert that database's changes yourself.
- You deleted the last migration's two files by hand and the next add looks wrong; how do you repair the snapshot?Run `dotnet ef migrations remove`. It sees that the snapshot no longer matches the last remaining migration's `BuildTargetModel`, reports that it assumes the migration was deleted manually, and rewrites the snapshot from that remaining migration. The next `migrations add` then diffs against the right model.
- dotnet ef migrations remove deletes whichever migration you name on the command line.
- Removing a migration also reverts the entity class changes that produced it.
- Deleting an applied migration's files is harmless once the database has the schema.
- Editing Up in a migration staging already ran updates staging on the next deploy.
- migrations remove --force is the normal way to undo a migration in production.