Entity Framework 6 Code First migrations - multipl

2019-04-22 17:30发布

问题:

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.

回答1:

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.