![](/img/trans.png)
[英]InvalidOperationException: No database provider has been configured for this DbContext.
[英]No database provider has been > configured for this DbContext. Configuring SQL Lite for Identity
我正在尝试为可复制的 repo 配置 MVP,但为此我使用的是 sql lite,因此它可以在没有配置的情况下运行,但是因为我自己遇到了错误。 我正在使用这个 repo 作为一种方式来证明其他东西不起作用但是我不明白为什么我会得到它?
我的上下文是:
namespace AuthorisationRepoMS.Dal {
public class ApplicationDBContext : IdentityDbContext<ApplicationUser> {
public ApplicationDBContext(DbContextOptions<ApplicationDBContext> options)
: base(options) {
}
protected override void OnModelCreating(ModelBuilder modelBuilder) {
base.OnModelCreating(modelBuilder);
}
}
}
我的 Startup.cs 如下
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services) {
services.AddEntityFrameworkSqlite().AddDbContext<ApplicationDBContext>();
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddDefaultUI()
.AddRoles<IdentityRole>()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDBContext>().AddClaimsPrincipalFactory<MyUserClaimsPrincipalFactory>(); //<---- add this line
services.AddControllersWithViews()
.AddDataAnnotationsLocalization();
services.Configure<CookiePolicyOptions>(options => {
options.CheckConsentNeeded = context => true; // consent required
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddSession(opts => {
opts.Cookie.IsEssential = true; // make the session cookie Essential
});
services.AddControllers(config => {
// using Microsoft.AspNetCore.Mvc.Authorization;
// using Microsoft.AspNetCore.Authorization;
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
});
services.Configure<IdentityOptions>(options => {
// Default Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
});
services.AddTransient<MISDBContextSeedData>();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
但我收到以下错误
System.InvalidOperationException: '没有为此 DbContext 配置数据库提供程序。 可以通过覆盖 DbContext.OnConfiguring 方法或在应用程序服务提供者上使用 AddDbContext 来配置提供者。 如果使用 AddDbContext,则还要确保您的 DbContext 类型在其构造函数中接受 DbContextOptions object 并将其传递给 DbContext 的基本构造函数。
提前致谢
您需要正确配置数据库上下文。 如果您使用的是 SQLite,那么它应该看起来像这样:
services.AddDbContext<ApplicationDBContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("DefaultConnection")));
这将假定您正确定义了一个连接字符串,例如 SQLite 的"Data Source=database.db"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.