Details on how to work with EF Database Migrations with .NET 6 APIs built using Wrapt.
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
.
dotnet ef
installed. To install it globally run:dotnet tool install -g dotnet-ef
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
cd YourBoundedContextName/src/YourBoundedContextName
dotnet ef migrations add InitialMigration
cd YourBoundedContextName/src/YourBoundedContextName
dotnet ef migrations add AddedColumnXToTableY
cd YourBoundedContextName/src/YourBoundedContextName
dotnet ef database update
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