6

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?

5
  • 1
    Database per dev is usually the answer here, but even that gets sticky when 2 devs make a new migrations and then try to commit. However, I think if you set a null initialiser when developing, EF doesn't check that the correct migrations have been applied. Something like this in the context constructor: 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) Commented Sep 29 at 8:21
  • 2
    First - you want to use your localdb for when you're developing and making these changes. Second - don't migrate the test database directly but running update-database. Do this as part of the release pipeline. Heck, even running migrations during application startup is probably a better idea than manually applying them. Commented Sep 30 at 17:49
  • @DanOrlovsky when using a localdb, how do I temporarily switch to another branch which has a radically different database schema without losing the test data that I entered for testing the previous branch? Commented Oct 1 at 8:15
  • 2
    @EMN database backup Commented Oct 1 at 9:02
  • 1
    We used to try to manage ordered Schema migrations like you're doing, but over time with a large number of developers it became too painful to manage without risk, so we moved to keeping DB schema completely in source control and (almost) never using migration scripts. Now we build the source schema into a dacpac, and compare that with the target DB, and run the resulting diff script. Every time a merge happens with a change to the DB project, we rebuild and re-compare. There are only a few instances where we have to modify the resulting script, and we have automated that as well. Commented Oct 6 at 17:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.