为 OpenIddict 配置 DbContext
Configure DbContext for OpenIddict
我正在使用 OpenIddict for JWT token authentication in my .NET Core app. I've followed this tutorial,但我现在收到以下错误:
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...
我在Startup.cs
中的ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
services.AddEntityFrameworkSqlServer()
.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration["Data:MyDbContext:ConnectionString"]));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<MyDbContext>()
.AddDefaultTokenProviders()
.AddOpenIddictCore<Application>(config => config.UseEntityFramework());
services.AddMvc();
// for seeding the database with the demo user details
//services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();
services.AddScoped<OpenIddictManager<ApplicationUser, Application>, CustomOpenIddictManager>();
}
不知道该怎么办,因为我在使用 AddIdentity
时无法添加 DbContext。
我的连接字符串很好,在添加 OpenIddict 之前一切正常。
更新
这是我的 appsettings.json
文件:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"Data": {
"DefaultConnection": {
"ConnectionString": "my connection string"
},
"SaleboatContext": {
"ConnectionString": "my connection string"
}
}
}
我的 DbContext:
public class ApplicationUser : IdentityUser { }
public partial class MyDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
由于 EntityFramework 核心 RC2 中的设计更改,您现在需要手动流动 DbContextOptions
或直接在 OnModelCreating
中配置连接字符串。
尝试将此构造函数添加到您的数据库上下文(应该派生自 OpenIddictContext
):
public partial class MyDbContext : OpenIddictContext<ApplicationUser> {
public MyDbContext(DbContextOptions options)
: base(options) {
}
}
我正在使用 OpenIddict for JWT token authentication in my .NET Core app. I've followed this tutorial,但我现在收到以下错误:
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...
我在Startup.cs
中的ConfigureServices
方法:
public void ConfigureServices(IServiceCollection services)
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
Configuration = builder.Build();
services.AddEntityFrameworkSqlServer()
.AddDbContext<MyDbContext>(options =>
options.UseSqlServer(Configuration["Data:MyDbContext:ConnectionString"]));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<MyDbContext>()
.AddDefaultTokenProviders()
.AddOpenIddictCore<Application>(config => config.UseEntityFramework());
services.AddMvc();
// for seeding the database with the demo user details
//services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();
services.AddScoped<OpenIddictManager<ApplicationUser, Application>, CustomOpenIddictManager>();
}
不知道该怎么办,因为我在使用 AddIdentity
时无法添加 DbContext。
我的连接字符串很好,在添加 OpenIddict 之前一切正常。
更新
这是我的 appsettings.json
文件:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"System": "Information",
"Microsoft": "Information"
}
},
"Data": {
"DefaultConnection": {
"ConnectionString": "my connection string"
},
"SaleboatContext": {
"ConnectionString": "my connection string"
}
}
}
我的 DbContext:
public class ApplicationUser : IdentityUser { }
public partial class MyDbContext : IdentityDbContext<ApplicationUser>
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
由于 EntityFramework 核心 RC2 中的设计更改,您现在需要手动流动 DbContextOptions
或直接在 OnModelCreating
中配置连接字符串。
尝试将此构造函数添加到您的数据库上下文(应该派生自 OpenIddictContext
):
public partial class MyDbContext : OpenIddictContext<ApplicationUser> {
public MyDbContext(DbContextOptions options)
: base(options) {
}
}