以另一种方式将OpenIddict实体注册到DbContext中 [英] Register OpenIddict entities into DbContext in another way
问题描述
除了调用之外,还有另一种方法可以将OpenIddict
所需的实体集注册到DbContext
上
services.AddDbContext<OpenIdDictDbContext>(options => {...})
中的options.UseOpenIddict();
.
Is there another way to register the entity sets needed by OpenIddict
onto a DbContext
except calling
options.UseOpenIddict();
in services.AddDbContext<OpenIdDictDbContext>(options => {...})
.
我对这种方法有麻烦,因为我有更多的DbContexts
,并且我想共享DbContextOptions
.
I have trouble with this approach, because I have more DbContexts
, and I want to share DbContextOptions
.
在.Net Core 2中,如果您可以对所有DbContexts
使用非通用DbContextOptions
,或者您必须对所有DbContexts
使用非通用DbContextOptions<T>
.因此,如果可能的话,我希望采用第一种方法.
In .Net Core 2, if you can use non generic DbContextOptions
for all DbContexts
OR you must have nongeneric DbContextOptions<T>
for all DbContexts
. So, I would like the first approach if it possible.
推荐答案
您可以从OnModelCreating
直接注册OpenIddict实体:
You can directly register the OpenIddict entities from OnModelCreating
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions options)
: base(options) { }
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Register the entity sets needed by OpenIddict.
// Note: use the generic overload if you need
// to replace the default OpenIddict entities.
builder.UseOpenIddict();
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
如果没有看到扩展名,请确保使用Microsoft.Extensions.DependencyInjection
,并且您的项目引用了OpenIddict.EntityFrameworkCore
.
If you don't see the extension, make sure you have a Microsoft.Extensions.DependencyInjection
using and that your project references OpenIddict.EntityFrameworkCore
.
这篇关于以另一种方式将OpenIddict实体注册到DbContext中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!