Database Migrations

Details on how to work with EF Database Migrations with .NET 5 APIs built using Wrapt.

Using Database Migrations

As of v0.9.0, an initial database migration will automatically be ran during every new:domain or 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 Windows, Bash, and PowerShell.

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

Database Migration Example

  1. Make sure you have dotnet ef installed. To install it globally run:

    dotnet tool install -g dotnet-ef
  2. Set your env variable (e.g. Development) that matches the environment name suffixed on Startup. The default ASPNETCORE_ENVIRONMENT is Production if none has been been set.


    $Env:ASPNETCORE_ENVIRONMENT = "Development"


    export ASPNETCORE_ENVIRONMENT=Development
  3. If you don't have any migrations for whatever reason, you can perform your first migration with something like the below where Ordering 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 Ordering.Infrastructure

  4. 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
  5. 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

Common Db Migration Error

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

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.