The Migrations Squash CLI Tool is a helper utility built on top of the official Entity Framework Core CLI tool. Its purpose is to squash multiple EF Core migrations into a single migration file when projects accumulate a large migration history.
Important: While this tool exists, squashing migrations is not a recommended practice in most cases. It should only be considered if you fully understand the risks outlined below.
- .NET 8 SDK or later
- Entity Framework Core CLI tool (
dotnet-ef) must be installed:dotnet tool install --global dotnet-ef
- Always apply all pending migrations to every environment before attempting to squash.
- Prefer keeping the migration history intact - database migrations are lightweight, and EF Core can handle a large number of them.
- Consider squashing only when you have an excessively long migration history (hundreds+) and you fully control all environments.
-
Searches for Initial Migration
- The tool inspects your migration files and prompts you to confirm the initial migration file name that will be recreated after squashing.
-
Generates Backups
- Creates a SQL script containing all existing migrations for safekeeping (saved in the Migrations folder).
- Builds an EF Core migration bundle (a self-contained executable) containing all migrations. This bundle can later be used to manage and apply the pre-squash migrations.
-
Deletes Old Migration Files
- After backup, all existing migration
.csandDesigner.csfiles are deleted.
- After backup, all existing migration
-
Re-creates the Initial Migration
- Initial migration is overridden, containing the entire schema definition.
dotnet tool install --global DotnetEfMigrationsSquashdotnet-ef-migrations-squash --project .\path\to\migrations\project --startup-project .\path\to\startup\project The tool supports all standard Entity Framework Core CLI options including:
--project/-p: The project to use--startup-project/-s: The startup project to use--context/-c: The DbContext to use--output-dir/-o: The directory to put files in--configuration: The configuration to use--framework: The target framework- And many more standard EF Core options
- Prompts for the Initial migration name confirmation that will be later overridden.
- Generates a SQL backup script of all migrations.
- Generates an EF Core bundle for safe rollback and migration management.
- Deletes all migration files.
- Recreates the Initial migration that now includes all previous migrations.
If something goes wrong during the squashing process:
- Restore from SQL backup: Use the generated
.sqlfile to recreate your database schema - Use the migration bundle: Run the generated
.exebundle file to apply the original migrations - Restore migration files: If you have source control, revert the changes to restore your original migration files
- This tool is for advanced scenarios only.
- Do not squash migrations unless absolutely necessary.
- Always ensure all environments (local, staging, production, CI/CD) are fully migrated to the latest version before running the tool.
- The
__EFMigrationsHistorytable in your database will NOT be updated after the new single migration is created. Initial migration is overwritten, and no changes are required. - Backup files are saved in your project's
Migrationsfolder with timestamps. All migrations before the squash operation can be applied from the backup bundle.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.