Steven Lemmens
Steven Lemmens

Reputation: 1491

Entity Framework Explicit Migrations

we have a project running Entity Framework 6.1 and we started using explicit migrations a while ago (in the past we would use automatic migrations) and we have run into the following situation.

  1. I create an explicit migration to create some indexes on fields. I do this in a seperate branch.
  2. A colleague of mine also starts an explicit migration to do some other work in his own branch.

Each branch is code reviewed and merged into the master branch when approved. But now we noticed that my explicit migration to create the indexes, was created on a different version of the model. Since it's a project with multiple developers, the model is always changing. So if we check what SQL code would be generated to update the database, we see that new columns/tables/... that have been added in the meantime while I was working on my branch are being dropped, that my indexes are then created and afterwards those columns would be added again.

How can we avoid this? What are we doing wrong in our workflow?

Upvotes: 0

Views: 65

Answers (1)

borisdj
borisdj

Reputation: 2529

With EF 6 each migration has metadata about last state of DB.
In EFCore this is much better done with separate file that has snapshot of DB.
Here are some good practices for Migrations in Team Environments:
https://msdn.microsoft.com/en-us/library/dn481501(v=vs.113).aspx
How to manage Migrations in a project with multiple branches?

Now your situation is pretty specific and I am not sure that any of these procedures has automatic solution for it.
One way I can think of is to have DB model not locally but on a server and that each developer targets that when creating migration.
However in the previous blogs shared DB was not considered best practice. You would need to figure out some hybrid procedure to comply with every advice. Good luck...

Upvotes: 1

Related Questions