Database Migrations

Details on how to work with EF Database Migrations with .NET 6 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 an in-memory db while in the Development environment, 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. Make sure your ASPNETCORE_ENVIRONMENT variable is et to literally anything except Development. This is because the in-memory database is used in the Development environment.

    Powershell

    $Env:ASPNETCORE_ENVIRONMENT = "anything"

    Bash

    export ASPNETCORE_ENVIRONMENT=anything
  3. If you don't have any migrations for whatever reason, you can perform your first migration with something like the below.

    cd YourBoundedContextName/src/YourBoundedContextName
    dotnet ef migrations add InitialMigration
  1. To add additional migrations, just change your migration description.

    cd YourBoundedContextName/src/YourBoundedContextName
    dotnet ef migrations add AddedColumnXToTableY
  2. 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):

    cd YourBoundedContextName/src/YourBoundedContextName
    dotnet ef database update

Common Db Migration Error

If you're getting an error like the below, make sure that you set your ASPNETCORE_ENVIRONMENT to anything but Development before running migrations. Literally, you could set it to anything and it should work.

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.

Additionally, if you are running Integration tests and see something like the below, make sure that you have migrations in your project.

System.InvalidOperationException : CommandText property has not been initialized
   at Npgsql.NpgsqlCommand.ProcessRawQuery(Boolean standardConformingStrings, Boolean deriveParameters)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)
   at Respawn.Checkpoint.ExecuteDeleteSqlAsync(DbConnection connection)
   at Respawn.Checkpoint.Reset(DbConnection connection)
   at RecipeManagement.IntegrationTests.TestFixture.ResetState() in IntegrationTests/TestFixture.cs
   at RecipeManagement.IntegrationTests.TestBase.TestSetUp() in IntegrationTests/TestBase.cs