We are planning to use a Gitflow workflow and a shared test database. We currently use EF 6.5.1 and have automatic migrations disabled.
Problem: if a developer runs Update-Database (or dotnet ef database update) while working on one branch, the shared database’s __MigrationHistory advances. Developers working on other branches then get this error when they are debugging the application:
The model backing the 'xyzContext' context has changed since the database was created
That branch becomes unusable until its migrations are reconciled with the updated database. To do this, we make sure to always make migrations on the develop branch, and then have all developers merge develop branch into the feature branch they are working on. This works for feature branches until two features use different versions of the same table. Not to mention, release branches, which use the same test database, cannot merge develop into them.
What is the solution for this?
I have explored keeping a local database for each developer, so that context changes will be isolated, and following the method mentioned in this article for resolving migration merging issues.
The problem I have with this approach is that, as far as I know, switching branches would require recreating the local database due to possible schema changes, which would erase all testing data used on the previous branch. Can recreating the local database be avoided?
Database.SetInitializer<xyzContext>(null);. You will have to ensure that new migrations leave the database in a backward compatible state (e.g. be careful when renaming or removing columns)