24

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.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
ygoe
  • 18,655
  • 23
  • 113
  • 210
  • There's no reason to call `AddEntityFrameworkWhatever` normally, that's the job of `.UseSqlServer` or `.UseNpgsql`. Are you sure you need that call? – Panagiotis Kanavos Jul 15 '20 at 14:28

1 Answers1

41

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")));
}
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • If I do that, I get problems with `MainDbContext` not being resolvable by the service provider in other parts of the application. Also, how should anybody know that a Postgres database should be used if there's no code for that? – ygoe Jul 15 '20 at 15:22
  • There *is* code, and I posted the link to the docs. `AddDbContext` registers a DbContext with the DI container which means you *shouldn't* get any DI error. How and where did you get that exception? What is the *full* exception text? How are those other parts of the application using DI? Are they using a different ServiceProvider perhaps? – Panagiotis Kanavos Jul 15 '20 at 15:29
  • Sorry, I misread the indentation and also removed the `AddDbContext` call. When removing the `AddEntityFrameworkNpgsql` call alone and leaving the rest, it works. Thank you! – ygoe Jul 15 '20 at 16:01