Using TFS Build to Deploy Entity Framework Database Migrations with Migrate.exe

Posted · 4 Comments

In the discussion thread for my DevOps Skills class at Pluralsight, I got a question about how to deploy database changes in a build using Entity Framework Migrations (aka. EF Code First Migrations).  It’s actually pretty easy and it all comes down to a tool that comes with Entity Framework called migrate.exe.  When you deploy your Entity Framework migrations using the Visual Studio Package Manager and call ‘Update-Database’, it ultimately ends up calling the same logic that is used by migrate.exe.  Pretty much anything that you can call from the command line can be hooked into a TFS automated build.

To keep everything easy and clean in your build, I’d recommend creating a directory in the artifacts folder of your build that will contain everything that you need to do your EF migrate calls.  What you’ll need are the DLLs and config file for your Entity Framework code plus the contents of the Entity Framework NuGet Package folder.  I usually create a folder called ‘for-deploy’ and then put the various pieces of my deployments in subfolders.  What you end up with is a folder that looks like the screenshot below.  You’ve got a folder in the artifacts directory of your build (“$(build.artifactstagingdirectory)\for-deploy\ef-database”) that has the DLLs and config file for my app’s entity framework code plus the bits for Entity Framework itself — most importantly, migrate.exe.

Deployment directory in my build that contains the Entity Framework DLLs for my app plus the contents of the Entity Framework NuGet package.

In the build definition, you just start with a standard default build for a Visual Studio project.  From there, you’ll add two Copy Files tasks and a Command Line task.  The first task copies the Entity Framework package contents.  Next, copy the Entity Framework data access DLLs for your project.  Finally, use a Command Line task to call Migrate.exe to deploy the database.

Create the for-deploy directory and deploy the database with Migrate.exe

Here are the arguments for the build steps.  The values here are example values and you’ll need to change them to match the names of your code and Entity Framework version that you’re using.  I’m also only specifying the non-default values for the steps.  If I don’t mention a value, don’t change the default.

Step Type: Copy Files
Step Name: Copy Entity Framework NuGet Package Contents Step
Source Folder: 
Target Folder:
Advanced / Over Write: True

Step Type: Copy Files
Step Name: Copy Application DLLs for my Entity Framework code
Source Folder: 
Target Folder:
Advanced / Over Write: True

Step Type: Command Line
Step Name: Deploy database using Migrate.exe
Benday.WebAppWithEF.Core.dll /startupConfigurationFile=Benday.WebAppWithEF.Core.dll.config
Advanced / Working folder: 

After you’ve added these steps to your build, be sure to save your changes then run the build.

NOTE: if the connection string in the app.config / web.config is not appropriate for your build environment, you’ll need to modify it before running the Migrate.exe step.

I hope this helps.



— Need help getting Entity Framework Migrations included into your build, test, and deploy process?  Want some assistance getting a DevOps flow flowing at your company?  Looking training on this stuff?  We can help.  Drop us a line at

4 Responses to "Using TFS Build to Deploy Entity Framework Database Migrations with Migrate.exe"
  1. jbssa says:

    Thank you for the response, sorry it’s taken so long to get back to you. But why are you using the 6.1 migrate exe for ef7? Does this mean that we need to get both versions for this to work? ef 7 uses the dotnet ef migrations command contained in ef.dll

    • Ben Day says:

      Hi jbssa —

      That sample is actually for EF running in full .NET not dotnet core. If you were doing dotnet core, then the deployment is different.


  2. Murali Krishnaraj says:

    Hi Ben

    This step applies the changes to the database. Is that the correct place to do database migration? If i have a release definition using this build that deploys the changes to different environment(Dev,Test & Prod), how to handle this scenario?.

    • Ben Day says:

      @Murali — in that case, it sounds like you want to do the database deployment from the release definition rather than the build definition. In the blog post, I suggest 3 steps that you should add to the build definition. To make this work from a Release, I’d leave Step #1 and Step #2 in the build definition and then move Step #3 to the release definition. NOTE: you’ll probably need to adjust the “Tool” and “Working Folder” paths in Step #3 because the source paths are probably a little different for the Release.


Leave a Reply


© 2016 Benjamin Day Consulting, Inc.
Brookline, MA
617-645-0188 | |