Below procedure has the benefit of working without doing anything with the DBs, __MigrationHistory can stay as-is. Also it will work if you have multiple different environments with different versions of the structure - provided you have the branches to match.
I turn the last migration into an initial migration. The trick is to use the oldest version of the code and DB that is in use, replace its last migration with a new initial migration and delete all previous migrations. Newer branches keep the more recent migrations so those will still work after merging to older branches.
So start in the OLDEST branch - PROD, normally - and do:
- Remove all but the last migration
- Remove the migration code in both the "Up" and "Down" methods in the last migration
- Change build action of the last migration to "None" to let EF ignore it
- Change active connection to point to a local DB database.
- Make sure this local DB database does not exist
- add-migration Initial
- Copy Up and Down code from the created "Initial" migration to the last migration
- Delete Initial migration
- Change build action of the last migration back to "Compile"
- Check in
- Merge changes up
- Test in DEV branch on LocalDB DB - it should do the new initial migration as well as the subsequent ones with no issues
- Test in main branch on the latest DB - it shouldn't do anything
Note above only works if you don't add stuff to the migrations that EF doesn't do itself. E.g. if you add DB views etc. than the newly created migration won't get those, it only gets the scripts EF generates based on your code.