Entity Framework Core has a good migration story for the Database, however it's not perfect, especially in a large team where DB schema changes are frequent. All migrations are increamental based on the last
*DbContextModelSnapshot.cs snapshot. When multiple migrations are happening in different branches, a lot of issues arise and we even asked EF team on Twitter on how to solve this problem. Click here for the tweet
To better understand the problem, let's build up a scenario in team project.
Let's see an example of problematic migration:
A / \ B1 C | | B2 | \ / (git merge) | D
B2 are out-of-sync of migration
C and when we merge
C back into main branch the
D migration will likely not work from correct DbContext model snapshot.
Traditionally, we would revert
C migration, merge branch with
B2 migrations and re-apply
C migration with correct DbContext model snapshot. Git doesn't help us resolving the problem because there are no conflicts from code perspective.
I found that the best way to solve this problem is to treat
*DbContextModelSnapshot.cs as binary file, forcing the developers to resolve the problem before they break migration in
Migrations folder with content:
This will prevent any non-incremental changes to
*DbContextModelSnapshot.cs and prevents breaking migrations after merging into
Subscribe to JK's dev blog
Get the latest posts delivered right to your inbox