
Install EF Core 3 Migration tools
The tooling of the Entity Framework Core Migrations comes via NuGet (and dotnet tooling).
1dotnet 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:
1dotnet tool install --global dotnet-ef --version 1.2.3
Update or reinstall EF Core 3 Migration tools
To update the tooling you can use:
1dotnet tool update --global dotnet-ef
If 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 Init
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.
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:
1dotnet ef database update
To 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_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
1dotnet ef migrations remove
References
See also: EFCore Migrations - Microsoft Docs

Comments