c# - 在 DbContext 选项配置中未调用“AddEntityFramework *' was called on the service provider, but ' UseInternalServiceProvider”

标签 c# asp.net-core .net-core entity-framework-core npgsql

我正在将 ASP.NET Core 应用程序从 Framework 2.2 升级到 3.1。它还使用 Entity Framework 核心。
在 Startup.ConfigureServices 方法中,有这样一段代码:

services.AddEntityFrameworkNpgsql()
    .AddDbContext<MainDbContext>(options => options
        .UseNpgsql(Configuration.GetConnectionString("MainDbContext")));
.NET Core 2.2 一切正常。使用 .NET Core 3.1,我在每次应用程序启动时都会收到此警告:

'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.


查找 UseInternalServiceProvider方法,看起来应该在 options 上调用传递主要服务提供者。不幸的是,此时服务提供者还不存在。它即将建成。
我不明白问题是什么以及这个警告想告诉我什么,但没能做到。我怎样才能让那个警告消失?网络还不知道这条消息。

最佳答案

删除 AddEntityFrameworkNpgsql . docs explain那 :

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.


实际Getting Started页面对于 Npgsql 显示不需要任何额外的东西:

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")));
}

关于c# - 在 DbContext 选项配置中未调用“AddEntityFramework *' was called on the service provider, but ' UseInternalServiceProvider”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62917136/

相关文章:

c# - 如何使用 json 通过 SSL 在 IIS 中托管的 WCF 端点上使用基本身份验证?

c# - 在依赖于同一类其他属性的属性中强制执行适当值的公认方法是什么?

调用 WCF 的 Java 应用程序返回 "Connection Reset",而 SoapUI 工作正常

c# - 如何简单地从 appsettings.json 读取

.net - 如何在通用 Windows 平台应用程序中更新一个特定的 dotnet 程序集?

c# - Swagger - 隐藏 api 版本参数

asp.net-core - blazorstrap InputType.select 未绑定(bind)

javascript - 在 ASP.NET MVC Core 中跨页面更改保留 JavaScript 设置

sql - 存储库模式困境 : Redundant Queries vs. 数据库往返

.net-core - 在包引用中使用 * 占位符时,dotnet restore 未更新到新版本