C# 无法解决此错误,";没有为此DbContext配置数据库提供程序;

C# 无法解决此错误,";没有为此DbContext配置数据库提供程序;,c#,entity-framework,asp.net-core,entity-framework-core,asp.net-core-2.1,C#,Entity Framework,Asp.net Core,Entity Framework Core,Asp.net Core 2.1,经过长时间的休息后,我最近回到了ASP.Net,并一直在尝试使用Core2.1开发web应用程序 我遵循了Microsoft网站上的一个“开始”指南,我与SQLExpress连接字符串的数据提供程序有问题 当我尝试访问SQLExpress时,不断出现错误“没有为此DbContext配置数据库提供程序” 完全错误 An unhandled exception occurred while processing the request. InvalidOperationException: No

经过长时间的休息后,我最近回到了ASP.Net,并一直在尝试使用Core2.1开发web应用程序

我遵循了Microsoft网站上的一个“开始”指南,我与SQLExpress连接字符串的数据提供程序有问题

当我尝试访问SQLExpress时,不断出现错误“没有为此DbContext配置数据库提供程序”

完全错误

An unhandled exception occurred while processing the request.
InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)

Stack Query Cookies Headers
InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.
Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
Microsoft.EntityFrameworkCore.DbContext.get_Model()
Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.get_EntityType()
Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.get_EntityQueryable()
Microsoft.EntityFrameworkCore.Internal.InternalDbSet<TEntity>.System.Linq.IQueryable.get_Provider()
Microsoft.EntityFrameworkCore.RelationalQueryableExtensions.FromSql<TEntity>(IQueryable<TEntity> source, RawSqlString sql, object[] parameters)
SQL_Connection_2.Pages.ContactModel.OnGet() in Contact.cshtml.cs
Microsoft.AspNetCore.Mvc.RazorPages.Internal.ExecutorFactory+VoidHandlerMethod.Execute(object receiver, object[] arguments)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeHandlerMethodAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeNextPageFilterAsync()
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Rethrow(PageHandlerExecutedContext context)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.RazorPages.Internal.PageActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
ConfigurationContext.cs

公共类配置上下文:DbContext{
公共配置上下文(){}
公共配置上下文(DbContextOptions选项):基本(选项){}
公共DbSet Persons{get;set;}
}
如果我将
ConfigurationContext.cs
中的代码更改为(以下),一切都可以正常工作,但这绕过了
appsettings.json
中的连接字符串,因此看起来好像
services.AddDbContext
Startup.cs
文件中执行

公共类配置上下文:DbContext{
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder){
optionsBuilder.UseSqlServer(@“服务器=。\SQLExpress;数据库=WebApp\u测试;受信任的\u连接=真;MultipleActiveResultSets=真;”);
}
}
有人能就如何修复此问题提供建议吗?理想情况下,我希望在
appsettings.json
文件中配置连接字符串


我花了很多时间试图解决这个问题,寻找发现和学习的时刻。

尝试使用您在onconfigurang方法中使用的连接字符串,因为您发布的内容不同

像这样-

  "ConnectionStrings": {
    "MyConnection": "Server=.\SQLExpress;Database=WebApp_Test;Trusted_Connection=True;MultipleActiveResultSets=true;"
  }

Startup.cs,添加ConnectionService.Set

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        ConnectionService.Set(configuration);
    }
ConnectionService.cs

public static string connstring = "";
public static string Set(IConfiguration config)
    {
        connstring = config.GetConnectionString("MyConnection");
    }
ConfigurationContext.cs

public class ConfigurationContext : DbContext {

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        optionsBuilder.UseSqlServer(ConnectionService.connstring);
    }

}

太好了,成功了,谢谢!我确实需要做一个小小的调整<代码>公共静态无效集(IConfiguration config)我的荣幸。只要一切正常,你可以随意调整。:)我非常不喜欢这种解决办法。为什么要创建一些虚拟服务来只保存这个连接字符串,而EF配置清楚地说明您应该能够将选项传递给dbcontexts构造函数?这段代码太脏了…你把答案贴出来吧。他想将connectionstring从appsettings.json传递到Dbcontext,而Startup.cs中的AddDbContext无法获得它。没有别的了。