经过长时间的休息,我最近回到了ASP.Net,并一直在尝试使用Core 2.1开发Web应用程序。
我遵循了Microsoft网站上的“入门”指南之一,并且SQL Express的连接字符串的数据提供程序遇到了问题。
当我尝试访问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)
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddDbContext<ConfigurationContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("MyConnection")); }); }
appsettings.json
{ "Logging": { "LogLevel": { "Default": "Warning" } }, "AllowedHosts": "*", "ConnectionStrings": { "MyConnection": "server=.\\SQLExpress;database=WebApp_Test;trusted_connection=true;" } }
ConfigurationContext.cs
public class ConfigurationContext:DbContext { public ConfigurationContext() { } public ConfigurationContext(DbContextOptions options) : base(options) { } public DbSet<Person> Persons { get; set; } }
[如果我将
ConfigurationContext.cs
中的代码更改为(以下),一切正常,但是这绕过了appsettings.json
中的连接字符串,因此似乎services.AddDbContext
在Startup.cs
文件中似乎没有执行。] >public class ConfigurationContext : DbContext { protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseSqlServer(@"Server=.\SQLExpress;Database=WebApp_Test;Trusted_Connection=True;MultipleActiveResultSets=true;"); } }
任何人都可以提供有关如何解决此问题的建议吗?
理想情况下,我希望在appsettings.json
文件中配置连接字符串。
我花了很多时间来解决这个问题,寻找发现和学习的机会。
经过长时间的休息,我最近回到了ASP.Net,并一直在尝试使用Core 2.1开发Web应用程序。我遵循了Microsoft网站上的“入门”指南之一,并且...
请尝试使用在OnConfiguring方法中使用的连接字符串,因为发布的内容不同。
喜欢这个-