
This article was written in 2019. Versions, tools and recommendations may have changed since then. Newer posts are collected under .NET.
Install EF Core 3 Migration tools
The tooling of the Entity Framework Core Migrations comes via NuGet (and dotnet tooling).
dotnet tool install --global dotnet-efThis installs the latest version of the EFCore dotnet tooling.
To specify a version of the tooling, you can use:
dotnet tool install --global dotnet-ef --version 1.2.3Update or reinstall EF Core 3 Migration tools
To update the tooling you can use:
dotnet tool update --global dotnet-efIf the tooling is already installed, it automatically reinstalls the tooling.
1Microsoft Windows [Version 10.0.18363.535]
2(c) 2019 Microsoft Corporation. All rights reserved.
3
4C:\Windows\system32>dotnet tool update --global dotnet-ef
5Tool 'dotnet-ef' was reinstalled with the latest stable version (version '3.1.0').
6
7C:\Windows\system32>Hint: in DevOps environments like Azure DevOps you should always use the update command in your build steps. If the tooling is installed, it updates or reinstalls the tooling. If the tooling is missing, is installs the tools.
The
installcommand would fail if the tooling is already installed, which is not the best behavior.
Setup Migration Context
My recommendation is to use a isolated migration project instead to keep all migration inside your application project.
- MyCSharp.Portal - the main application domain project
- MyCSharp.Portal.Database.Migrations - the database migration project
The migration project must be an executable project like a console application (netcoreapp3.1)!
To run migrations, you can create your own migration context. This context should inherit from your main application database context.
1 public class MigrationEFCoreDbContext : EFCoreDbContext
2 {
3 public MigrationEFCoreDbContext(DbContextOptions<MigrationEFCoreDbContext> options)
4 : base(options)
5 {
6
7 }
8 }The idea behind the migration application and migration context is:
- separate executable migration application (e.g. DevOps)
- avoid risky automatic migrations
- isolated place to run specific migration operations
The migration Program.cs then could look like:
1 public class Program
2 {
3 public static void Main(string[] args)
4 => CreateHostBuilder(args).Build().Run();
5
6 // EF Core uses this method at design time to access the DbContext
7 public static IHostBuilder CreateHostBuilder(string[] args) =>
8 Host.CreateDefaultBuilder(args)
9 .ConfigureServices((hostContext, services) =>
10 {
11 services.AddDbContext<MigrationEFCoreDbContext>(o =>
12 o.UseSqlServer("ConnectionString"));
13 });
14 }Create Migration
To create a migration you can use the migration add <name> command:
1// creates a new migration with the title "Init"
2dotnet ef migrations add Init1C:\_s\MyCSharp\MyCSharpNET\src\MyCSharp.Portal.Database> dotnet ef migrations add Init
2Build started...
3Build succeeded.
4info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
5 Entity Framework Core 3.1.0 initialized 'MyCSharpEFCoreMigrationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
6Done. To undo this action, use 'ef migrations remove'Afterwards you will see a Migrations folder in your migrations project. This folder contains all migration-specific files.
1 Directory: C:\_s\MyCSharp\MyCSharpNET\src\MyCSharp.Portal.Database\Migrations
2
3
4Mode LastWriteTime Length Name
5---- ------------- ------ ----
6-a---- 24.12.2019 18:59 45422 20191224175935_Init.cs
7-a---- 24.12.2019 18:59 46048 20191224175935_Init.Designer.cs
8-a---- 24.12.2019 18:59 46010 MigrationEFCoreDbContextModelSnapshot.csUpdate Migration - and DevOps
You can run your migration changes with following command:
dotnet ef database updateTo specify a migration, you can pass the name or the signature of your migration:
1dotnet ef database update Init // or
2dotnet ef database update 20191219180921_InitHint: the database connection string must be part of the migration project application settings. Those settings can also be part of your devops projects like environment variables!
A lot of people export the migrations as script (
dotnet ef migrations script) and use a DACPAC file for database DevOps.
Remove Migration
To remove the latest migration you can use
dotnet ef migrations removeReferences
See also: EFCore Migrations - Microsoft Docs
Related articles

- 2 min read
Unit Testing with Entity Framework Core 3
This post covers the basic structure for unit tests with Entity Framework Core. Note: Unit tests are only there to test the respective …

- 11 min read
Containerizing .NET in 2026 Without a Dockerfile
Most .NET Dockerfiles repeat the same five steps: restore, publish, select the matching Microsoft runtime image, copy the output and start …

- 12 min read
Problem Details in ASP.NET Core and .NET 11
builder.Services.AddProblemDetails() registers infrastructure. It does not create an error contract. An ASP.NET Core request can fail during …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
