Entity Framework Code First Migrations in multiple branch scenarios

The starting information how the situation related to merging migrations can be read in this great post from Anders Abel.

I just have an extension to it for a specific situation.

Simple case merge migration

This is the case when the merged migration (from the source branch) is the last migration on the target branch. The code to that is fairly easy described in the above linked post. You only need to re-scaffold the code first model (in the designer file of the migration). Just a notice: don’t use the “force” option here, so you don’t add all the changes to your merged migration. Example below:

Add-Migration MergedMigrationClassName

Complicated case merge migration

If your migration is not the last one in the target branch, you have to do a couple of other things.

  1. Clean solution
  2. Delete database (for simplicity, it would be enough to update/downgrade the database to the target merged migration)
  3. Exclude migrations after the merged migration from the project
  4. Re-scaffold the merged migration as described above
  5. Include one of the excluded migrations
  6. Re-scaffold it as described above
  7. Repeat step 5 and 6 till you included all migrations

This way was working for me, however it is not so intuitive. Please comment on my post if you find an easier way.

Advertisements

About Tamas Nemeth

Husband and proud father of two daughters in Nürnberg. I'm working as a Senior Software Developer and an enthusiastic Clean-Coder. I spend most of my free time with my family (playing, hiking, etc...). I also play table-tennis and badminton sometimes...
This entry was posted in Technical Interest and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s