Using Entity Framework with git and having multiple branches updating a model can be quite challenging.
Here is a concrete example:
- develop branch is on code first migration v1
- feature X updates model to add fields to an entity, adding migration v2
- feature Y also updates model to add fields on an entity, adding migration v3
- feature X is merged into develop
- feature Y is merged, coming on top of previous one
First, both feature migrations are independent and both built on top of v1:
EF will be able to process them, but what will happen is this:
Changes noted here as incoming will bring back migration v2 changes when running “Add-Migration” again, even if they are present.
It basically means EF does not see v2 because v3 was generated with v2 not being present in the branch at the time of adding a migration.
This has to do with data found in migration RESX file, which contains Base 64 binary encoded info to keep track of link between each.
Bottom line: to avoid having this to happen:
- Rebuild migration v3 on top of v2 in develop branch by deleting v3 after getting v2 from develop
- Build a model update feature that everyone merge into their branch one updated with their model change
- merge feature X into feature Y only for db change, then add your fields in feature Y so that new fields rely on migration v2