Search code examples
asp.net-core-2.0openiddict

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


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 ?


Solution

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