How to merge and manage Entity Framework code first migration, when a different team work on the same application with a source control environment.
Introduction
I know there are several articles on Entity Framework Code First approach. But This article intended for those, who face problem to execute Update-Database command in entity framework after merging the different branch with the change in the same domain object.Background
There are many contents available in the web to demonstrate how Entity Framework Code First work and I assumes you know that and this is the prerequisite to grab the abstract of this article. If you don't then you can dig into the links:- http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
- http://www.asp.net/mvc/overview/older-versions/hands-on-labs/aspnet-mvc-4-entity-framework-scaffolding-and-migrations
- https://msdn.microsoft.com/en-us/data/jj591621
When we manage a source repository and work in a different team, Entity Framework may cause a problem when two developer change in same domain object in their local code base and Add-Migration to local database and then push code to remote branch.
I'll demonstrate how to generate above situation step by step and how we can resolved it.
Scenario to generate migration problem
Considering that, In a development team, team 'X' works on his local code base with local database and team 'Y' also work on his local code base and local database. Team 'X' add a column to a domain object "Student" and add-migration '***_Address column added to student table' and update-database in locally. On the other hand Team 'Y' also add a column to same "Student" domain object and add-migration '***_EnrollmentDate column added to student table' and update-database locally.Now team 'X' commit their local change and push to remote repository. And team 'Y' take a pull from the remote branch and get all changes that are done by team 'X' and resolved the conflict.
Points of Interest
Now when team 'Y' build the solution and try to execute command 'Update-Database' in package manager console. Stop! for a while and see what happen. All database change is done perfectly but package manager console shown bellow message:This is because Entity Framework notice that domain model snap shot has been changed by pulling and merging code base from remote branch.
How could we handle this situation
It's time to add a blank migration to update snap shot of current domain model for team 'Y'. To add a blank migration team 'Y' need to add 'IgnoreChanges' attibute in package manager console. One more things, team 'Y' need to consider a name for this migration which was never used in this solution.Now execute Update-Database command in package manager console and everything is done to handle this situation. Now time to commit and push this code to remote branch, So that other team could use this code base without any hastle.