Entity Framework Migrations

The purpose of generating Entity Framework migrations is to apply database schema changes consistently across Staging, UAT and Production environments.

The proposed method would involve developers generating migration a migration script whenever changes are made to the Entity Framework model in their projects. The DevOps team would apply that migration script to update the Azure environments. The services and applications being developed themselves should not have permissions to modify the environment.

Requirements

The following assumes the migration C# files, and the SQL scripts, are being generated for a .NET service/application with a working database connection and DbContext.

Install the following NuGet packages to the project:

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools

Install the Entity Framework Command Line Tools

The method for generating the migration scripts will be different, according to whether it’s being done through PowerShell or Visual Studio’s Package Manager Console.

In the Windows Terminal, the Entity Framework tools are installed globally:

dotnet tool install --global dotnet-ef

The tools can also be installed for the Visual Studio project using the Package Manager Console:

Install-Package Microsoft.EntityFrameworkCore.Tools
Update-Package Microsoft.EntityFrameworkCore.Tools

Create a migration:

If there is more than one project in the Visual Studio solution (such as unit tests), it will be necessary to specify the project to generate the migration source files from, using the ‘-Project’ or ‘-P’ parameter. e.g.

Add-Migration TestMigration -Project DotNet-Core-6-MVC

This should create a Migrations directory in the Visual Studio project.

Generating SQL Script

The following generates a SQL script from the last created migration:

dotnet ef migrations script

Or, in the Package Manager Console:

script-migration -P DotNet-Core-6-MVC

Again, the project name must be defined, if there are multiple in the currently-opened solution.

Generating an Entity Framework Migrations Bundle

A bundle is an executable file that applies the migrations. Might be very useful for DevOps, and might be possible to execute through Azure PowerShell. This can be done with the folloing command:

dotnet ef migrations bundle --self-contained

To apply this to a specific database from a specific migration:

.\bundle.exe [Migration Name] --connection "[Connection String]"

Migrations and Docker

One method is to add something like the following command to the Dockerfile:

RUN dotnet ef database update

Applying Migrations on Azure

There are several potential solutions:

  • Apply migrations by running generated SQL scripts through PowerShell and the AZ command line.
  • Create another Docker container as an agent to apply the migrations (tricky).
  • Create a Logic App on Azure that contains the SQL script and can be triggered after a build is tested.
  • Add the migration to Tasks under the ‘Automation’ section for the relevant App Service.

Suggested Method

The solution being used has something to do with YAML-defined Azure deployment pipelines.

Further Information

Further Inormation (DevOps)

#Migration #EntityFramework #Database #DevOps