EF Core Remove Migration — Complete Guide
In this tutorial, you'll learn about EF Core Remove Migration. We cover key concepts, practical examples, and best practices.
You add a migration but realize the model change is wrong, incomplete, or unnecessary. The migration is not yet applied to the database. You want to remove it and regenerate. dotnet ef migrations remove deletes the last unapplied migration.
Wrong
// Manually delete the migration files
// But forget to update the ModelSnapshot — now snapshot is out of sync
Output: Next migration is incorrect because ModelSnapshot.cs still reflects the old migration. Schema drift and errors.
Right
# Remove the last migration (not applied to database)
dotnet ef migrations remove
Output: The migration .cs file and the .Designer.cs file are deleted. The ModelSnapshot.cs is reverted to the previous state.
If the migration was already applied to the database:
# First roll back the database
dotnet ef database update PreviousMigration
# Then remove the migration
dotnet ef migrations remove
Force remove (even if applied — use with caution):
dotnet ef migrations remove --force
Prevention
- Use
dotnet ef migrations removeinstead of manually deleting migration files. - Run
dotnet ef migrations listto see which migrations are applied vs pending. - Only remove the latest migration — remove older migrations by reverting to them.
- Remove a bad migration immediately before anyone else applies it.
- Check
ModelSnapshot.csafter removal to confirm it reverted correctly. - Use
--forceonly when you know the database has been rolled back. - Always review the diff after removing a migration.
Common Mistakes with core migration remove
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world EF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Learn more about EF Core migrations at DodaTech.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro