Edit: a colleague of mine discovered an easier with to do this, I left my original answer at the bottom for completeness.
(VERY IMPORTANT) migrations in live environment must not conflict with ones in your current branch, otherwise you need to redo all your migrations and resolve data model change conflicts by hand.
- restore your development database with live environment data
- run
update-database
, it should run migrations from your branch, and complain about 'unable to update database to match the current model blah blah..'
- run
add-migration MergeBranchBToMaster -ignoreChanges
, this will create an empty migration.
- run
update-database
again
- push your changes
The magic in step 3 basically tells EF to shutup about mismatched models, hence be very sure that your migrations do not conflict with ones in live environment. If they do, you can always create SQL scripts for pushing the missing migrations (which is actually the preferred method).
Original Answer
I have found a fairly straight-forward solution based on @Ladislav Mrnka's answer. This will work with live environment[1], you just have to be careful not to change any deployed migrations.
Before Merge, take note of the migration you added (MyMigration),
and its previous migration (BaseMigration)
Merge branches in git
Open Package Manager Console, and run: UPDATE-DATABASE -TargetMigration:BaseMigration. This will revert your database to the state before any of the conflicted migrations are applied
Delete your local migration (MyMigration)
Run: UPDATE-DATABASE. This will apply all newer migrations done in other branches.
Run: ADD-MIGRATION MyMigration. This will re-generate your local migration based on current state of database, like git -rebase.
Run: UPDATE-DATABASE. Update database with you local migration.
This also works if you have multiple local migrations, but it will merge them all into a single one.
[1] by working with live environment, I mean that the generated migration can be applied to live environment which may already have some/all of the other branches' migrations applied. The steps themselves are purely for development purpose.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…