本文介绍了无法为"OpenIddictApplication"创建DbSet,因为此类型不包含在上下文模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试自定义OpenIddictApplication表,因此我成功了.

I am trying to customize the OpenIddictApplication table and i am succeeded about that.

我的问题是,当我尝试生成令牌时,出现以下错误无法为'OpenIddictApplication'创建DbSet,因为此类型不包含在上下文模型中".

My problem is that when i try to generate token i got the following error "Cannot create a DbSet for 'OpenIddictApplication' because this type is not included in the model for the context".

这是我的服务配置:

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc().AddXmlSerializerFormatters();
    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    services.AddDbContext<ApiHubContext>(options =>
    {
        options.UseMySql(Configuration.GetConnectionString("DefaultConnection"));
        options.UseOpenIddict<ApplicationClient, ApplicationAuthorization, ApplicationScope, ApplicationToken, long>();
    });
    services.AddOpenIddict()
        .AddCore(options =>
        {
            options.UseEntityFrameworkCore().UseDbContext<ApiHubContext>();
        })
        .AddServer(options =>
        {
            options.UseMvc();
            options.EnableTokenEndpoint("/connect/token");
            options.AllowClientCredentialsFlow().AllowRefreshTokenFlow();
            options.SetAccessTokenLifetime(TimeSpan.FromMinutes(10));
            options.SetRefreshTokenLifetime(TimeSpan.FromMinutes(4));
            options.AcceptAnonymousClients();
        })
        .AddValidation();

    services.AddAuthentication(options =>
    {
        options.DefaultScheme = OpenIddictValidationDefaults.AuthenticationScheme;
    });
} 

我的自定义DbContext:

My Custom DbContext:

 public class ApiHubContext : DbContext
    {
        public ApiHubContext(DbContextOptions options)
            : base(options) { }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);
            //builder.UseOpenIddict();
            builder.UseOpenIddict<ApplicationClient, ApplicationAuthorization, ApplicationScope, ApplicationToken, long>();
        }
    }

    public class ApplicationClient : OpenIddictApplication<long, ApplicationAuthorization, ApplicationToken>
    {
        public bool IsActive { get; set; }
        public string Remarks { get; set; }
    }

    public class ApplicationAuthorization : OpenIddictAuthorization<long, ApplicationClient, ApplicationToken> { }
    public class ApplicationScope : OpenIddictScope<long> { }
    public class ApplicationToken : OpenIddictToken<long, ApplicationClient, ApplicationAuthorization> { }

我已经检查了此链接.

有什么我想念的吗?

推荐答案

您只是忘记将OpenIddict EF Core存储配置为使用您的自定义实体:

You simply forgot to configure the OpenIddict EF Core stores to use your custom entities:

services.AddOpenIddict()
    .AddCore(options =>
    {
        options.UseEntityFrameworkCore()
               .UseDbContext<ApiHubContext>()
               .ReplaceDefaultEntities<ApplicationClient, ApplicationAuthorization, ApplicationScope, ApplicationToken, long>();
    });

这篇关于无法为"OpenIddictApplication"创建DbSet,因为此类型不包含在上下文模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 22:58