Entity Framework bundle migrations, set start point

Viewed 113

In multiple projects I would like to bundle all migrations, because over time the amount of Migrations is huge.

Then I delete the existing migrations and on a clean database I would create a new migrations via Add-Migrations 'Base entities' for example.

But on the existing instances I want to set that the 'Base entities' migration can be skipped.

Can anybody tell me how I could do this?

Thanks in advance!

1 Answers

I have used the following process:

  1. Take a note of the latest migration id which is the filename. eg. 20210719105339_latest

  2. Ensure that all databases that rely on the migrations have been updated with the 20210719105339_latest migration.

  3. Delete all the migrations from the Migrations folder.

  4. Create a new code-first migration using the CLI: core: dotnet ef migrations add Base_Entities. framework: Add-Migration -Name Base_Entities.

  5. Update the genereated migration filenames to match the deleted latest migration: 20210719105339_latest.cs,20210719105339_latest.Designer.cs

  6. Update the migration id in the designer file. core: the Migration class attribute: [Migration("20210719105339_latest")]. framework: the IMigrationMetadata.Id property: string IMigrationMetadata.Id { get { return "20210719105339_latest"; } }

As the database holds a record of the migrations that have been run in the __EFMigrationsHistory (framework: __MigrationHistory) table it will now appear as if the migration you have just created has already been run.

You will need to be careful with this process if you have any migrations that are creating objects that are not generated by EF migrations (eg. views, stored procedures, functions, full text catalogs) as this setup will be lost unless you copy it to the new base migration.

Related