无法为“ OpenIddictApplication”创建DbSet,因为此类型不包含在上下文模型中

时间:2019-02-20 10:05:31

标签: asp.net-core-2.0 openiddict

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

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

这是我的服务配置:

    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:

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

我已经检查了this link

有什么我想念的吗?

1 个答案:

答案 0 :(得分:0)

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

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