Details on how to work with EF Database Migrations with .NET 5 APIs built using Wrapt.
As of v0.9.0, an initial database migration will automatically be ran during every
add:bc command, so you should have an initial migration ready for you from the get go.
There is a built in timeout or you might not have the Entity Framework CLI installed, so you may need to run migrations manually after the fact.
Because Wrapt projects use a multi-environment setup, you will need to set an
ASPNETCORE_ENVIRONMENT before running
dotnet ef commands to let
the host builder locate the appropriate settings file to use. Here's some examples for
If you'd like to track the feature request for an inline command in EF, you can follow the Github issue.
When running migrations, make sure your
UseInMemoryDatabase setting is set to
false in your appsettings. This should be the case by default on all environments that are not
Make sure you have
dotnet ef installed. To install it globally run:
dotnet tool install -g dotnet-ef
Set your env variable (e.g. Development) that matches the environment name suffixed on Startup. The default
Production if none has been been set.
$Env:ASPNETCORE_ENVIRONMENT = "Development"
If you don't have any migrations for whatever reason, you can perform your first migration with something like the below where
is the solution name for this bounded context. Note that you should be in the
src directory of the bounded context that you want to run your migrations on.
dotnet ef migrations add "InitialMigration" --project Ordering.Infrastructure --startup-project Ordering.WebApi --output-dir Migrations
Your migrations should be under the project you are running them on, so in this case
To add additional migrations, you can do something like this with
Ordering again being the solution name for this bounded context. Make sure that your
ASPNETCORE_ENVIRONMENT has been set.
dotnet ef migrations add "added X column to y entity" --project Ordering.Infrastructure --startup-project Ordering.WebApi --output-dir Migrations
To push the updates to your database, you can run this (note that we're using the shorthand options in this case, but it doesn't matter which is used in any of the examples):
dotnet ef database update -p Ordering.Infrastructure -s Ordering.WebApi
If you're getting an error like the below, make sure that your connection string is set in your
appsettings.json file and
UseInMemoryDatabase is set to
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.Migrations.IMigrator'. This is often because no database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.