Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using VSTS Release Definition to run Entity Framework Code Database Migration

I am writing a .NET Core and Entity Framework Core Application using Code-First Migrations.

I want to be able to deploy it to an Azure Web App using Visual Studio Team Services Build and Release Definitions

I want to be able to run the Database Migration as part of the Release Definition using the script

dotnet ef database update

I've done this via a Command Prompt action in the Release Definition

However I always get the message

No executable found matching command "dotnet-ef"

I've tried making sure that this command is running in the same directory as a .cproj file

I've also tried running a

dotnet restore 

as a previous command prompt task, and this gives an error

The folder 'C:\a\r1\a\Drop\s\src\xxxxx' does not contain a project to restore even though it does.

Has anybody tried to do a Code First Migration as part of a Team Services Release Definition step?

My other option was to run the migration as part of the Web Application itself but I wanted to run it via the Release process rather than run it in the Application.

like image 437
TimBunting Avatar asked Apr 06 '17 15:04

TimBunting


1 Answers

You need to use Hosted VS2017 agent instead of Hosted agent. My steps:

  1. Edit csproj file to add this code below and check in changes (Refer to .NET Command Line Tools article)

Code:

<ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.1" PrivateAssets="All" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0" />
  </ItemGroup>
  1. Command line step/task (Tool: dotnet; Arguments: --version)
  2. Command line step/task (Tool: dotnet; Arguments: restore)
  3. Command line step/task (Tool: dotnet; Arguments: ef --version)
  4. Queue build with Hosted VS2017 agent.
like image 125
starian chen-MSFT Avatar answered Sep 27 '22 17:09

starian chen-MSFT