Run EF Core Migrations in Azure DevOps
December 06, 2020 by Anuraj
Azure DevOps EFCore
This post is about running Entity Framework Core migrations on Azure DevOps which helps you to continuously deploy your database changes to your staging or your QA environments or even to production - even though I won’t recommend it. We will be using Entity Framework Core Code first approach.
For this blog post I am using an Web API application - a todo list - which is using SQL Server as backend. I am creating EF core migrations and committing them to source control - which is important - in this case GitHub. I am building the SQL Scripts from EF Core migrations code which is committed in source control. And as part of Release pipeline I am executing them in the Azure SQL Server.
Here is my build pipeline looks like.
It is normal .NET Core build pipeline, but I have added few more steps to build and generate EF Core migrations.
Create Manifest file - This is again a dotnet command which will help you to create a manifest file using the
dotnet new tool-manifest
command. This is required because I am installing thedotnet ef
command locally instead of installing it globally.Install EF tool - This command install the
dotnet ef
tool locally, so that I can build the SQL Script from migrations script. This is step is using thedotnet tool install dotnet-ef
.Create SQL Scripts - This step will generate SQL scripts using
dotnet ef
command with the following command -dotnet ef migrations script --output $(Build.SourcesDirectory)/SQL/tododbscript.sql --idempotent --project $(Build.SourcesDirectory)/src/TodoApi.csproj --context TodoApiDbContext
. In this command the--idempotent
parameter is important. Otherwise it might not work the way it is expected.Publish Artifacts : SQL Scripts - This step will publish the SQL Scripts as artifacts generated by the
Create SQL Scripts
step.
Here is the complete YAML script.
This script will create two artifacts - the drop zip file and SQL Script file.
Next I created a Release pipeline which take this artifacts and deploy it Azure Web App and SQL Server. This step is optional, you can deploy it along with build pipeline. But it is recommended to use Release pipeline, so that we can control the number of deployments to the environments.
First step will deploy the drop
folder to web app using Web Deploy. Second I am adding Azure SQL Database deployment
task. In which I am configuring authentication mode which is SQL Server, username, password and database. And select Deploy type as SQL Script File
and select the SQL Script file - $(Build.SourcesDirectory)/SQL/tododbscript.sql
.
You can configure the trigger to deploy it when a build completes or you can manually do it. Here is the Release pipeline completed.
This way you can configure your application database deployment using Azure DevOps and Entity Framework Core migrations - you need to make sure you’re committing the migration scripts to source control. Unlike running dotnet ef database update command - the script command won’t create the Databases. You need to create an empty database and configure it in the SQL Task in release pipeline.
Happy Programming :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub