【问题标题】:Can't resolve "Cannot create DbSet for 'OpenIddictEntityFrameworkCoreApplication" because this type is not included in the model for the context."无法解决“无法为‘OpenIddictEntityFrameworkCoreApplication’创建 DbSet”,因为此类型未包含在上下文的模型中。
【发布时间】:2022-01-08 10:41:27
【问题描述】:

我将授权代码流与 OpenIddict 库一起使用,我收到“InvalidOperationException”并显示以下消息:“Cannot create DbSet for 'OpenIddictEntityFrameworkCoreApplication' because this type不包含在上下文模型中。”消息。尝试运行应用程序时会抛出此消息。

Error Message

我在网上找到了一些解决方案,但没有一个能够解决我的问题。

这是我的启动配置和 DbContext。我正在使用 ASPNet Core 6 中的默认身份模型。

Program.cs

builder.Services.AddOpenIddict()

    // Register the OpenIddict core components.
    .AddCore(options =>
    {
        // Configure OpenIddict to use the Entity Framework Core stores and models.
        // Note: call ReplaceDefaultEntities() to replace the default entities.
        options.UseEntityFrameworkCore()
               .UseDbContext<ApplicationDbContext>();
    })

    // Register the OpenIddict server components.
    .AddServer(options =>
    {
        // Enable the client credentials flow.
        options.AllowClientCredentialsFlow();

    // Enable the authorization code flow.
    options.AllowAuthorizationCodeFlow().RequireProofKeyForCodeExchange();

    // Enable the token endpoint.
    options.SetAuthorizationEndpointUris("/connect/authorize")
        .SetTokenEndpointUris("/connect/token");

    // Register the signing and encryption credentials.
    options.AddDevelopmentEncryptionCertificate()
          .AddDevelopmentSigningCertificate();

    // Register scopes (permissions)
    options.RegisterScopes("api");

    // Register the ASP.NET Core host and configure the ASP.NET Core options.
    options.UseAspNetCore()
          .EnableTokenEndpointPassthrough()
          .EnableAuthorizationEndpointPassthrough();
    })

    // Register the OpenIddict validation components.
    .AddValidation(options =>
    {
        // Import the configuration from the local OpenIddict server instance.
        options.UseLocalServer();

    // Register the ASP.NET Core host.
    options.UseAspNetCore();
    });

ApplicationDbContext.cs

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options): base(options) {   }

我还没有找到我收到此问题的原因。任何帮助都会很棒。谢谢。

【问题讨论】:

    标签: c# authentication oauth-2.0 asp.net-identity openiddict


    【解决方案1】:

    问题出在我的启动配置中。我发现对以下方法的 2 次调用是这样的......

    builder.Services.AddDbContext<ApplicationDbContext>(options => 
      options.UseSqlServer(connectionString));
    builder.Services.AddDbContext<ApplicationDbContext>(options =>
    {
        options.UseSqlServer(connectionString);
        options.UseOpenIddict();
    });
    

    我删除了第一个方法调用,异常消失了。

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 1970-01-01
      • 2019-08-16
      • 2019-07-13
      • 2019-07-10
      • 1970-01-01
      • 2018-10-20
      • 1970-01-01
      • 2019-01-04
      相关资源
      最近更新 更多