Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 6 Code First migrations - multiple branches for production

In my project we have a branch model that has a separate development branch and has a separate branch for each release. It may look like this:

dev         ______       ______
           /      \     /      \
master  --+---+----+---+---+----+----+--- (...)
           r1  \______/  r2 \_______/

So we develop on dev merge it to master and then we create a release branch (r1, r2, ...).

We want to use EF 6 (manual not automatic) migrations but we have a question that we don't know how to answer.

Imagine this:

dev         _(1)__       ____(4)
           /      \     /      \
master  --+---+----+---+---+----+-(5)*-+--- (...)
           r1  \_(2)__/  r2 \(3)______/

Each number is a migration. They have been added to the source control on each branch AND have been applied to the databases of production instances of our project (we support multiple releases for some time just for fixes) so we cannot downgrade them, they can go only Up(). Asterisk marks the point in time we want to analyze. We want migrations to work this way:

  • r1 database has only migration (2) applied
  • r2 database has (1), (2) and (3) applied
  • master database has (1), (2), (4) and (5) applied
  • dev database has (1), (2) and (4) applied

Moreover:

  • we cannot lose a bit of data in our database
  • it has to be possible for r1 database to be updated to r2 (or any other future release) database with no errors so it has the same database structure as all r2 databases (created as r2 or updated from earlier versions) that corresponds to its code first model.

Can that be done? If yes, how?

If you need more details, please ask.

like image 633
pinus.acer Avatar asked Jul 10 '14 20:07

pinus.acer


People also ask

How do I turn on automatic migrations in Entity Framework?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations –EnableAutomaticMigration:$true command (make sure that the default project is the project where your context class is).

How do I run all migrations in Entity Framework?

To manage migrations, you must first install the EF Core command-line tools. If the DbContext is in a different assembly than the startup project, you can explicitly specify the target and startup projects in either the Package Manager Console tools or the . NET Core CLI tools.


1 Answers

I could try to explain what you can do in each case, but it's quite complex and I would not be able to explain it as well as it's explained here:

Code First Migrations in Team Environments

Although it's complex and you'll have to refer to this document frequently to merge the branches, if you follow this document you'll see that it solves all your questions.

Disclaimer: I know SO should be self contained, but the explanation is so complex and long that I can't even add an abstract here. I hope that the linked MSDN docs don't dissapear.

like image 167
JotaBe Avatar answered Oct 09 '22 21:10

JotaBe