Cannot create a DbSet for 'OpenIddictApplication' because this type is not included in the model for the context

独自空忆成欢 提交于 2021-01-28 15:39:02

问题


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

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".

Here is my service configuration:

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

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> { }

I have already checked this link.

Is there anything i have missed ?


回答1:


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


来源:https://stackoverflow.com/questions/54783658/cannot-create-a-dbset-for-openiddictapplication-because-this-type-is-not-inclu

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