Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Entity Framework 3 - Migrations

Entity Framework 3 - Migrations
December 19, 2019 3 min read BEN ABT
.NET EFCore

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).

Shell
dotnet tool install --global dotnet-ef

This installs the latest version of the EFCore dotnet tooling.

To specify a version of the tooling, you can use:

Shell
dotnet tool install --global dotnet-ef --version 1.2.3

Update or reinstall EF Core 3 Migration tools

To update the tooling you can use:

Shell
dotnet tool update --global dotnet-ef

If the tooling is already installed, it automatically reinstalls the tooling.

Shell
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 install command 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.

C#
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:

C#
 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:

Shell
1// creates a new migration with the title "Init"
2dotnet ef migrations add Init
Shell
1C:\_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.

Shell
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.cs

Update Migration - and DevOps

You can run your migration changes with following command:

Shell
dotnet ef database update

To specify a migration, you can pass the name or the signature of your migration:

Shell
1dotnet ef database update Init // or
2dotnet ef database update 20191219180921_Init

Hint: 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

Shell
dotnet ef migrations remove

References

See also: EFCore Migrations - Microsoft Docs

Related articles

Unit Testing with Entity Framework Core 3

Dec 15, 2019 - 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 …

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026 - 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 …

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026 - 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 …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

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.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026
Problem Details in ASP.NET Core and .NET 11

Problem Details in ASP.NET Core and .NET 11

Oct 5, 2026
Resilience in .NET with Microsoft.Extensions.Resilience

Resilience in .NET with Microsoft.Extensions.Resilience

Sep 21, 2026
What Matters in ASP.NET Core 11

What Matters in ASP.NET Core 11

Sep 14, 2026

Tags

.NET (192) Csharp (96) ASP.NET Core (48) Azure (41) DevOps (17) Docker (14) Microsoft (14) EF Core (11) Microsoft MVP (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Security (9) Synology (9) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7) Bicep (6)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact