以另一种方式将OpenIddict实体注册到DbContext中

时间:2017-09-19 12:10:27

标签: openiddict

除了调用之外,还有另一种方法可以将OpenIddict所需的实体集注册到DbContext options.UseOpenIddict();中的services.AddDbContext<OpenIdDictDbContext>(options => {...})

我在使用这种方法时遇到问题,因为我有更多DbContexts,我想分享DbContextOptions

在.Net Core 2中,如果您可以对所有DbContextOptions使用非通用DbContexts,则必须为所有DbContextOptions<T>使用非通用DbContexts。所以,如果可能的话,我想要第一种方法。

1 个答案:

答案 0 :(得分:1)

您可以直接从OnModelCreating注册OpenIddict实体:

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