Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

azure devops - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.EFindebug **
    • Target folder: $(build.artifactstagingdirectory)/EF
  2. Copy Files "task"
    • Source folder: $(build.sourcesDirectory)packagesEntityFramework.6.1.3ools
    • Contents: migrate.exe
    • Target folder: $(build.artifactstagingdirectory)EFContoso.EFindebugindebug
  3. Batch script "task"
    • Path: $(build.sourcesdirectory)_DeployMigrateEFContext.bat
    • Arguments: $(build.artifactstagingdirectory)EFContoso.EFindebug 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...