gpt4 book ai didi

c# - ASP.NET Core 2.1 无法识别配置扩展

转载 作者:行者123 更新时间:2023-12-03 23:17:41 26 4
gpt4 key购买 nike

我正在尝试设置我的项目,但我总是收到此错误:

Cannot convert lambda to type 'ServiceLifetime' because it is not a delegate type.

在我的启动类(class)中,我做了:

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

还有

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ServiceProvider>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings")));
}

我什至尝试安装包 Microsoft.Extensions.Configuration; 但仍然遇到相同的错误!

最佳答案

问题是您正在使用 ServiceProvider作为您的DbContextTypeservices.AddDbContext<TContext> 。替换services.AddDbContext<ServiceProvider>services.AddDbContext<YourDbContext>如下:

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<YourDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("ConnectionStrings")));
}

关于c# - ASP.NET Core 2.1 无法识别配置扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56052816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com