The managing of a database schema has seen several advances over the years in the .NET stack. Circa 2010, Visual Studio began shipping with a new Database Project, and some SKUs included data compare tools, which helped compare data schema and the data itself. Around the same time, Entity Framework was launched, at first only offering support in Model and Database First approaches, it was soon offered with the now, super popular, Code First approach.
The introduction of Code First was in reaction to people who wanted to keep their time in writing code and out of a database designer. So if we’re going to stay out of the database designer, how do we manage our database’s schema? Enter Migrations. Code First Migrations can analyze your model and automatically create schema update files to get your database updated with your latest model changes. When I started using Migrations, I was in love. It was seriously time saving and super easy to use.
Soon enough, after touting, and bragging about how I found the new hotness, I introduced migrations on a project that I was working with a few other people on. The first week went…well…not good. I really thought this whole thing was gearing up for me to end up with some serious egg on my face.
The Pain, Oh The Pain
So, in order to understand why there was pain, we first need to understand how migrations work. Before doing any deep dives, I made assumptions. My main assumption that when I started with code first was that with the ‘Add-Migration’ command, the magic going on was that the little minions inside were going out to my database and making notes about what’s different between the database and my code first model. I mean, this is logical, right, this is how we track schema changes using the Schema Compare tool in the past. Well, wrong-o. Code first doesn’t care about your database at all.
In reality, migrations are doing their comparisons against the last known state of the model. It does that this by making use of a snapshot file. You see, whenever you issue an ‘Add-Migration <some migration>’ command, migrations creates a snapshot of your entire model. So the next time you issue an ‘Add Migration <some other migration>’, migrations compares the current state of the model to the snap shot that was created in ‘some migration’. Seeing the problem yet?
So this all works fine and dandy for you. So now let's imagine that we have two developers Sarah and Roger. Sarah does some work, creates a migration,
SarahMigration1, and checks in. Roger, gets latest, and sees Sarah’s migration,
SarahMigration1. He begins doing some work and creates a migration of his own,
RogerMigration1. So behind the scenes, migrations compares Roger’s changes against the snap shot and spits out a migration file. Sweet. But Roger isn’t done working and doesn’t check in his changes yet. Meanwhile, Sarah needs to create another migration, so she issues an ‘
Add Migration SarahMigration2’ command. So what happens? Migrations doesn’t know about
RogerMigrationOne, or the snapshot that was created along with it, so migrations compares Sarah’s current model against the only snapshot it knows about, which is
SarahMigration1. So a migration file is created that looks good, since Sarah knows nothing about Roger’s changes, her new migration looks and seems right. What happened behind the scenes is what’s going to cause some problems. The snapshot that was created in Sarah’s last command, doesn’t include Rogers’s changes. So Sarah checks in and Roger gets latest. Since migrations lists migration by date, he will see
SarahMigration2. Ok…seems ok. Oh, but it’s so not. When Roger goes to create his second migration, migrations is going to compare his current model against the latest snap shot,
SarahMigration2, but the snapshot in
SarahMigration2 knows nothing about
RogerMigrationOne changes, so migrations is going to generate all of Roger’s changes, including the changes that are already defined in
I like to call this the, ‘Oww, My Toes’ scenario. So the obvious solution might be, just delete
RogerMigration1. While that might work in some scenarios, if you’ve updated your database, you’re going to be in a world of hurt if you do that… so don’t do that.
To fix this scenario, you have 2 options:
Option One: Generate A Blank Migration
- Write all of your pending model changes to a migration and update your database.
- Get latest from source control.
- Issue an ‘Add-Migration <some name> -IgnoreChanges’ command.
This method will result in a blank migration file being added to your codebase, but this solution is quick and easy, so the tradeoff of having blank files may be worth it. Note that this option will not work for you if you have already gotten latest before writing your pending model changes to a migration, so you may want to get into the habit of writing your changes to a migration before getting latest.
Option Two: Regenerate the Snapshot
- Find the migration before the one that matches your current database status.
- Issue command ‘Update-Database – TargetMigration [migrationbeforecurrent]’
- This will revert your database ‘back to good’ if you will.
- At this point, you can delete your migration that is in between the other two as well as the latest migration that had the duplicate migration code.
- Issue Command ‘Add-Migration MyNewMigration’
- This will create a new migration that
- Issue Command ‘Update-Database
This should bring the database base back to good and inline with the migrations files, as well as creating a new snapshot of the database that matches the current code first model. Note that this option only works if the latest migration exists in your local workspace only. It cannot have been committed to source control.
Best Practices For Teams Using Migrations
- Turn off Automatic Migrations – This isn’t just for teams, this is for pretty much anyone using migrations. Automatic migrations takes all control away from the developer on how the database is migrated. You should be in control of the migration files to avoid, ‘It just deleted all my data scenarios’ (Yes, it will do this. Be extra sure you have automatic migration off for anything in Production)
- Get latest often – Always having the latest helps to reduce the number of times this scenario occurs.
- Check in often – On the contrary, be a good team member by getting new migration checked in as soon as possible.
- Designate a migration master – This certainly isn’t going to work for all teams, and it comes with some risk that the migration master becomes a bottle neck for the whole team. But basically, the other developers make changes to the context model, but the actual migration files are always created by one person, that person is always guaranteed to have the correct snapshot of the model if they are the only one creating migration files. This is the only option to completely eliminate the ‘Oww, My Toes’ scenario.