Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run Entity Framework's migrate.exe from Visual Studio Online?

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?

like image 844
user11081980 Avatar asked Jan 30 '16 21:01

user11081980


People also ask

How do I run Entity Framework migration?

Open the Package Manager Console from Tools → Library Package Manager → Package Manager Console and then run the enable-migrations command (make sure that the default project is the project where your context class is).

How do I run migrations in Visual Studio?

From the Tools menu, select NuGet Package Manager > Package Manager Console. The enable-migrations command creates a Migrations folder in the ContosoUniversity project, and it puts in that folder a Configuration. cs file that you can edit to configure Migrations.

Where is ef6 EXE located?

The ef6.exe command line tool is found in the tools/ subfolder of the EntityFramework NuGet package.

How can add migration in ASP NET MVC?

Open the Package Manager Console. Select Tools > NuGet Package Manager > Package Manager Console. The Enable-Migration command creates the Migrations folder, which contains a script to initialize the database. Open the Configuration.


1 Answers

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
like image 129
user80498 Avatar answered Sep 22 '22 16:09

user80498