{
  "$type": "site.standard.document",
  "canonicalUrl": "https://johnnyreilly.com/posts/vsts-and-ef-core-migrations",
  "description": "Learn how to migrate Entity Framework database migrations during an ASP.NET Core project deployment with a console app using VSTS and Azure.",
  "path": "/posts/vsts-and-ef-core-migrations",
  "publishedAt": "2018-06-24T00:00:00.000Z",
  "site": "at://did:plc:yy3apqjlms24kso7ahn7lbmb/site.standard.publication/3mova7c4nho2b",
  "tags": [
    "azure devops",
    "sql server"
  ],
  "textContent": "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.\n\n\n\nIf 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.\n\nIt'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.\n\nDigression 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:\n\n> 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.\n\nConsole Yourself\n\nFirst things first, we need a console app added to our solution. Fire up PowerShell in the root of your project and:\n\nNext we need that project to know about Entity Framework and also our DbContext (which I store in a dedicated project):\n\nAdd our new project to our solution: (I always forget to do this)\n\nYou should now be the proud possessor of a .csproj file that looks like this:\n\nReplace the contents of the Program.cs file with this:\n\nThis code takes the database connection string passed as an argument, spins up a db context with that, and migrates like it's the Serengeti.\n\nBuild It!\n\nThe 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:\n\nThere's various ways to accomplish this which I wont reiterate now. I recommend YAML.\n\nDeploy It!\n\nNow 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:\n\nIn the following folder:\n\nDo note that the command uses the ConnectionStrings.MyAwesomeProjectDatabaseConnection variable which you need to create and set to the value of your connection string.\n\nGive It A Whirl\n\nLet's find out what happens when the rubber hits the road. I'll add a new entity to my database project:\n\nAnd reference it in my DbContext:\n\nLet's let EF know by adding a migration to my project:\n\nCommit my change, push it to VSTS, wait for the build to run and a deployment to take place.... Okay. It's done. Looks good.\n\nLet's take a look in the database:\n\nIt's there! We are migrating our database upon deployment; and not in our ASP.Net Core app itself. I feel a burden lifted.\n\nWrapping Up\n\nThe 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!",
  "title": "VSTS and EF Core Migrations"
}