VSTS and EF Core Migrations
John Reilly
June 24, 2018
Let me start by telling you a dirty secret. I have an ASP.Net Core project that I build with VSTS. It is deployed to Azure through a CI / CD setup in VSTS. That part I'm happy with. Proud of even. Now to the sordid hiddenness: try as I might, I've never found a nice way to deploy Entity Framework database migrations as part of the deployment flow. So I have [blushes with embarrassment] been using the Startup of my ASP.Net core app to run the migrations on my database. There. I said it. You all know. Absolutely filthy. Don't judge me.
If you care to google, you'll find various discussions around this, and various ways to tackle it. Most of which felt like too much hard work and so I never attempted.
It's also worth saying that being on VSTS made me less likely to give these approaches a go. Why? Well, the feedback loop for debugging a CI / CD setup is truly sucky. Make a change. Wait for it to trickle through the CI / CD flow (10 mins at least). Spot a problem, try and fix. Start waiting again. Repeat until you succeed. Or, if you're using the free tier of VSTS, repeat until you run out of build minutes. You have a limited number of build minutes per month with VSTS. Last time I fiddled with the build, I bled my way through a full month's minutes in 2 days. I have now adopted the approach of only playing with the setup in the last week of the month. That way if I end up running out of minutes, at least I'll roll over to the new allowance in a matter of days.
Digression over. I could take the guilt of my EF migrations secret no longer, I decided to try and tackle it another way. I used the approach suggested by Andre Broershere:
> I worked around by adding a dotnetcore consoleapp project where I run the migration via the Context. In the Build I build this consoleapp in the release I execute it.
Console Yourself
First things first, we need a console app added to our solution. Fire up PowerShell in the root of your project and:
Next we need that project to know about Entity Framework and also our DbContext (which I store in a dedicated project):
Add our new project to our solution: (I always forget to do this)
You should now be the proud possessor of a .csproj file that looks like this:
Replace the contents of the Program.cs file with this:
This code takes the database connection string passed as an argument, spins up a db context with that, and migrates like it's the Serengeti.
Build It!
The next thing we need is to ensure that this is included as part of the build process in VSTS. The following commands need to be run during the build to include the MigrateDatabase project in the build output in a MigrateDatabase folder:
There's various ways to accomplish this which I wont reiterate now. I recommend YAML.
Deploy It!
Now to execute our console app as part of the deployment process we need to add a CommandLine task to our VSTS build definition. It should execute the following command:
In the following folder:
Do note that the command uses the ConnectionStrings.MyAwesomeProjectDatabaseConnection variable which you need to create and set to the value of your connection string.
Give It A Whirl
Let's find out what happens when the rubber hits the road. I'll add a new entity to my database project:
And reference it in my DbContext:
Let's let EF know by adding a migration to my project:
Commit my change, push it to VSTS, wait for the build to run and a deployment to take place.... Okay. It's done. Looks good.
Let's take a look in the database:
It's there! We are migrating our database upon deployment; and not in our ASP.Net Core app itself. I feel a burden lifted.
Wrapping Up
The EF Core team are aware of the lack of guidance around deploying migrations and have recently announced plans to fix that in the docs. You can track the progress of this issue here. There's good odds that once they come out with this I'll find there's a better way than the approach I've outlined in this post. Until that glorious day!
Discussion in the ATmosphere