I am writing XUnit integration tests for my ASP.NET Core Web API and need to replace the production DbContext registration with an EFCore InMemoryDatabase for testing, while keeping the original configuration in Program.cs.
I configure this in my CustomWebApplicationFactory as follows:
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
var context = services.FirstOrDefault(descriptor => descriptor.ServiceType == typeof(DartsDbContext));
if (context != null)
{
services.Remove(context);
var options = services.Where(r => (r.ServiceType == typeof(DbContextOptions))
|| (r.ServiceType.IsGenericType && r.ServiceType.GetGenericTypeDefinition() == typeof(DbContextOptions<>))).ToArray();
foreach (var option in options)
{
services.Remove(option);
}
}
services.AddDbContext<DartsDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
});
});
}
However, I receive the error:
One or more errors occurred. (Services for database providers 'Npgsql.EntityFrameworkCore.PostgreSQL', 'Microsoft.EntityFrameworkCore.InMemory' have been registered in the service provider. Only a single database provider can be registered in a service provider
If I delete the line of code in Program.cs that adds the original DbContext, it works because only one DbContext exists. I want to keep the production DbContext registration in Program.cs and have CustomWebApplicationFactory replace it only during tests.