Register OpenIddict entities into DbContext in another way

帅比萌擦擦* 提交于 2019-12-11 05:35:29

问题


Is there another way to register the entity sets needed by OpenIddict onto a DbContext except calling options.UseOpenIddict(); in services.AddDbContext<OpenIdDictDbContext>(options => {...}).

I have trouble with this approach, because I have more DbContexts, and I want to share DbContextOptions.

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.


回答1:


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);
    }
}

If you don't see the extension, make sure you have a Microsoft.Extensions.DependencyInjection using and that your project references OpenIddict.EntityFrameworkCore.



来源:https://stackoverflow.com/questions/46300110/register-openiddict-entities-into-dbcontext-in-another-way

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!