Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'AddEntityFramework*' was called on the service provider, but 'UseInternalServiceProvider' wasn't called in the DbContext options configuration

I'm upgrading an ASP.NET Core application from Framework 2.2 to 3.1. It also uses Entity Framework Core.

In the Startup.ConfigureServices method, there is this code:

services.AddEntityFrameworkNpgsql()
    .AddDbContext<MainDbContext>(options => options
        .UseNpgsql(Configuration.GetConnectionString("MainDbContext")));

Everything was fine with .NET Core 2.2. With .NET Core 3.1, I get this warning on every application start:

'AddEntityFramework*' was called on the service provider, but 'UseInternalServiceProvider' wasn't called in the DbContext options configuration. Remove the 'AddEntityFramework*' call as in most cases it's not needed and might cause conflicts with other products and services registered in the same service provider.

Looking up the UseInternalServiceProvider method, it looks like that should be called on the options to pass on the main service provider. Unfortunately, at this point, the service provider does not exist yet. It is just about to be built.

I don't understand what the problem is and what this warning wants to tell me, but failed to do. How can I make that warning go away? The web doesn't know about this message yet.

like image 951
ygoe Avatar asked Jul 15 '20 14:07

ygoe


1 Answers

Remove AddEntityFrameworkNpgsql. The docs explain that :

Calling this method is no longer necessary when building most applications, including those that use dependency injection in ASP.NET or elsewhere. It is only needed when building the internal service provider for use with the method. This is not recommend other than for some advanced scenarios.

The actual Getting Started page For Npgsql shows there's no need for anything extra :

simply place the following in your ConfigureServices method in Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    // Other DI initializations

    services.AddDbContext<BloggingContext>(options =>
            options.UseNpgsql(Configuration.GetConnectionString("BloggingContext")));
}
like image 148
Panagiotis Kanavos Avatar answered Sep 20 '22 03:09

Panagiotis Kanavos