© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2mo ago•
5 replies
Fattouh

Only one database provider can be registered while setting up integration tests.

asp.netvisual studio
I hit this EF Core error while setting up integration tests.
Message: 
System.InvalidOperationException : Services for database providers 'Microsoft.EntityFrameworkCore.SqlServer', 'Microsoft.EntityFrameworkCore.InMemory' have been registered in the service provider. Only a single database provider can be registered in a service provider. If possible, ensure that Entity Framework is managing its service provider by removing the call to 'UseInternalServiceProvider'. Otherwise, consider conditionally registering the database provider, or maintaining one service provider per database provider.
Message: 
System.InvalidOperationException : Services for database providers 'Microsoft.EntityFrameworkCore.SqlServer', 'Microsoft.EntityFrameworkCore.InMemory' have been registered in the service provider. Only a single database provider can be registered in a service provider. If possible, ensure that Entity Framework is managing its service provider by removing the call to 'UseInternalServiceProvider'. Otherwise, consider conditionally registering the database provider, or maintaining one service provider per database provider.

public class CustomWebApplicationFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        base.ConfigureWebHost(builder);

        builder.UseEnvironment("IntegrationTesting");

        builder.ConfigureServices(services =>
        {
            ServiceDescriptor? serviceDescriptor = services.SingleOrDefault(service => service.ServiceType == typeof(DbContextOptions<PersonsDbContext>));

            if (serviceDescriptor is not null) services.Remove(serviceDescriptor);

            services.AddDbContext<PersonsDbContext>(options =>
            {
                options.UseInMemoryDatabase("IntegrationTestingDatabase");
            });

        });
    }
}
public class CustomWebApplicationFactory : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        base.ConfigureWebHost(builder);

        builder.UseEnvironment("IntegrationTesting");

        builder.ConfigureServices(services =>
        {
            ServiceDescriptor? serviceDescriptor = services.SingleOrDefault(service => service.ServiceType == typeof(DbContextOptions<PersonsDbContext>));

            if (serviceDescriptor is not null) services.Remove(serviceDescriptor);

            services.AddDbContext<PersonsDbContext>(options =>
            {
                options.UseInMemoryDatabase("IntegrationTestingDatabase");
            });

        });
    }
}

So I’m trying to understand this:
- Why does targeting
DbContextOptions<TContext>
DbContextOptions<TContext>
lead to a provider conflict in some setups? I saw some GitHub discussions about this but couldn’t fully connect the dots. Add API to remove provider configuration. #35126
- Would registering the
DbContext
DbContext
conditionally based on the environment be a good way to avoid this provider conflict, rather than removing DbContext-related services?
GitHub
Add API to remove provider configuration. · Issue #35126 · dotnet...
After upgrading to .NET 9 I get the following error on db.Database.EnsureCreated(); in the following code: using (var scope = sp.CreateScope()) { var scopedServices = scope.ServiceProvider; var db ...
Add API to remove provider configuration. · Issue #35126 · dotnet...
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

❔ Only a single database provider can be registered in a service provider
C#CC# / help
3y ago
❔ WebApplicationFactory and integration tests
C#CC# / help
3y ago
Populating IOptions in integration tests
C#CC# / help
3mo ago
How do I write integration tests
C#CC# / help
13mo ago