Categories
Azure DevOps

3. Build Pipeline with Azure DevOps – Generate EF Core Migration Script

This is the third article in the series of Build Pipeline with Azure DevOps. Other articles in the series are:

1. Build Pipeline with Azure DevOps – The Basics
2. Build Pipeline with Azure DevOps – AppSettings.json Transformations
3. Build Pipeline with Azure DevOps – Generate EF Core Migration Script
4. Build Pipeline with Azure DevOps - Publish and Bundle Artifacts

In this article we will discuss on how to generate a SQL script from the migrations that Entity Framework generated for us. When we create a new migration in EF it generates a couple of files but none of those are SQL scripts that can be run against a SQL Server.

We will add two steps to generate a SQL file and then later on in the release pipeline we will use the “Run Db Migrations” step to execute the generated SQL against the Live SQL Server. Let’s look at the build steps:

- task: DotNetCoreCLI@2
  displayName: 'Initialize EntityFrameworkCore'
  inputs:
    command: custom
    custom: tool
    arguments: 'install --global dotnet-ef'

- task: DotNetCoreCLI@2
  displayName: 'Create migration'
  inputs:
    command: custom
    custom: ef
    arguments: 'migrations script -i -p webapiStartupProjName -o $(Build.ArtifactStagingDirectory)/Migrations/migration.sql'

In first task above we are initializing the EF Core tool so we could run dotnet ef commands against our data project. In the second task we run the custom ef command and generate a SQL migration in staging directory.

The next step would be to publish and bundle the Artifacts and make them available for our release pipeline.