Removing all migrations or regenerating them has drawbacks so we took an approach we merged all older migrations.
It require a bit of scripting. You read about the details here https://www.bokio.se/engineering-blog/how-to-squash-ef-core-migrations/ and see the scripts here https://github.com/bokio/EFCoreTools/tree/main/MigrationSquasher
The basic is the following steps though (copied from the blog post):
Overview of our approach
- Create a new fresh database from the old migrations (We will use
this for comparison later)
- Find a suitable target migration to be
the new initial (We picked one about 3 months old)
- Write a script to
merge the Up() methods of all earlier migrations into the Up()
method of a new migration. We ignored Down() because we don't use it
for old migrations.
- Generate this migration and add it to the
project. In our case we called it
20200730130157_SquashedMigrations1.cs. We used the snapshot from the
target migration we had picked.
- Generate a 2nd prep migration that
inserts into the migrations history that
20200730130157_SquashedMigrations1.cs has already run. We called
this 20200730130156_SquashedMigrations1_prep.cs. Note the slightly
smaller timestamp on that one to make sure it runs before the real
migrations.
- Delete the old migrations
- Point our config to a new
database and run the migrations.
- Compare that the schema we generate
is equal using the Sql Schema Compare in Visual Studio.
- Work through
the issues until we have equal schemas. This part is a bit
complicated but I will get back to it.
- Merge and