6

I have set up continuous integration for my project with Visual Studio Online build definitions.

When it comes to deploying my database (to an Azure test environment) I just build my SQL Server Database Project with the right publishing settings.

But I want to switch to Entity Framework's code first approach and leverage the migration feature, which requires me to call migrate.exe.

My question is - how could I run migrate.exe from VSO build definitions?

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
user11081980
  • 3,059
  • 4
  • 30
  • 48

3 Answers3

10

We've succesfully implemented an automated EF code first migration at deploy time on top of TFS Build vNext in the following way:

It basically involves 3 steps (per EF-context):

  1. Copy EF-project to a staging directory
  2. Copy migrate.exe in same folder (migrate.exe requires to be placed right next to assembly containing EF migrations)
  3. Execute migrate.exe

In detail:

  1. Copy Files "task"
    • Source folder: $(build.sourcesdirectory)
    • Contents: Contoso.EF\bin\debug\ **
    • Target folder: $(build.artifactstagingdirectory)/EF
  2. Copy Files "task"
    • Source folder: $(build.sourcesDirectory)\packages\EntityFramework.6.1.3\tools
    • Contents: migrate.exe
    • Target folder: $(build.artifactstagingdirectory)\EF\Contoso.EF\bin\debug\bin\debug
  3. Batch script "task"
    • Path: $(build.sourcesdirectory)_Deploy\MigrateEFContext.bat
    • Arguments: $(build.artifactstagingdirectory)\EF\Contoso.EF\bin\debug Contoso.EF.dll [SQL-SERVER-INSTANCE] [DbName] System.Data.SqlClient

The MigrateEFContext.bat file assembles the migrate.exe-command with its arguments:

SET EFDir=%1
SET EFContext=%2
SET connStringDataSource=%3
SET connStringInitialCatalog=%4
SET connectionProviderName=%5

%EFDIR%\migrate.exe %EFContext% /ConnectionString:"Data Source=%connStringDataSource%;Initial Catalog=%connStringInitialCatalog%;Integrated Security=true" /connectionProviderName:%connectionProviderName% /verbose
user80498
  • 735
  • 1
  • 7
  • 15
7

I assume you are using vNext build, add a "Nuget Installer" task in your build definition first to restore the Entity Framework during the build. Migrate.exe will be installed in \packages\EntityFramework.\tools folder. Then add a "Command Line" task to run the migrate.exe. Enter “\packages\EntityFramework.\tools\migrate.exe" in "Tool" area and the arguments in "Arguments" field.

Eddie Chen - MSFT
  • 29,708
  • 2
  • 46
  • 60
  • 3
    As I know, wildcards cannot be used in Command Line task. So how to remove dependence from EF version? – alexey May 12 '16 at 10:23
  • i'm getting the following error. Any ideas? Could not load file or assembly 'EntityFramework, Version=6.0.0.0 Running on vNext build system. – TWilly May 16 '16 at 13:27
  • @TWilly Refer to this link for how to use the migrate.exe: https://msdn.microsoft.com/en-sg/data/jj618307.aspx – Eddie Chen - MSFT May 17 '16 at 02:04
2

You can also look at executing your migrations at App startup time.

add the following to your Application_Start() event in global.asax

var configuration = new Configuration();
var migrator = new DbMigrator(configuration);
migrator.Update();

This will fire the migrations at each application startup. you could also wrap with conditional logic to control how it is fired.

Kevin Kraus
  • 283
  • 1
  • 2
  • 11
  • Yup. As described here https://blogs.msdn.microsoft.com/webdev/2014/04/08/ef-code-first-migrations-deployment-to-an-azure-cloud-service/ – JackMorrissey May 13 '16 at 22:04
  • 5
    I believe this to be a very dangerous approach as you won't know about migration issues until the first call to your website. I would certainly advocate a specific upgrade step to the database as an explicit step. – The Senator Mar 29 '17 at 08:36