【问题标题】:Configure DbContext for OpenIddict为 OpenIddict 配置 DbContext
【发布时间】:2016-08-29 19:20:00
【问题描述】:

我在我的 .NET Core 应用程序中使用 OpenIddict 进行 JWT 令牌身份验证。我关注了this tutorial,但现在收到以下错误:

InvalidOperationException: No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider...

Startup.cs 中我的ConfigureServices 方法:

        public void ConfigureServices(IServiceCollection services)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json");
        Configuration = builder.Build();

        services.AddEntityFrameworkSqlServer()
             .AddDbContext<MyDbContext>(options =>
                 options.UseSqlServer(Configuration["Data:MyDbContext:ConnectionString"]));

        services.AddIdentity<ApplicationUser, ApplicationRole>()
            .AddEntityFrameworkStores<MyDbContext>()
            .AddDefaultTokenProviders()
            .AddOpenIddictCore<Application>(config => config.UseEntityFramework());

        services.AddMvc();

        // for seeding the database with the demo user details
        //services.AddTransient<IDatabaseInitializer, DatabaseInitializer>();
        services.AddScoped<OpenIddictManager<ApplicationUser, Application>, CustomOpenIddictManager>();
    }

不知道该怎么做,因为我在使用 AddIdentity 时无法添加 DbContext。

我的连接字符串很好,在添加 OpenIddict 之前一切正常。

更新 这是我的appsettings.json 文件:

    {
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Verbose",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Data": {
    "DefaultConnection": {
      "ConnectionString": "my connection string"
    },
    "SaleboatContext": {
      "ConnectionString": "my connection string"
    }
  }
}

我的 DbContext:

public class ApplicationUser : IdentityUser { }

public partial class MyDbContext : IdentityDbContext<ApplicationUser>
{

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
}

【问题讨论】:

  • 您能分享您的数据库上下文吗?我想确保连接字符串正确地流向 EntityFramework。
  • 我的连接字符串不在我的 dbcontext 中,它在我的 appsettings.json 中
  • 连接字符串的配置位置并不重要。我只是想确保你的上下文有正确的构造函数来允许外部配置。

标签: entity-framework asp.net-core openid-connect aspnet-contrib openiddict


【解决方案1】:

由于 EntityFramework Core RC2 中的设计更改,您现在需要手动传输 DbContextOptions 或直接在 OnModelCreating 中配置连接字符串。

尝试将此构造函数添加到您的数据库上下文(应该从 OpenIddictContext 派生):

public partial class MyDbContext : OpenIddictContext<ApplicationUser> {
    public MyDbContext(DbContextOptions options)
        : base(options) {
    }
}

【讨论】:

  • 谢谢你的朋友,我会尽快检查一下
  • 我遇到的问题是 ApplicationDbContext 未定义,不知道如何继续实施。
  • 糟糕,复制/粘贴错误。当然,您必须将其命名为您的上下文类,因为它是一个构造函数。很抱歉。
  • 这行得通,谢谢。我想您不想花几分钟时间来帮助 .net 核心新手度过一些成长的痛苦吗?
  • 如果您有任何问题,请随时使用asp.net-core 标签在 SO 上提问,我会看看 ;)
猜你喜欢
  • 2017-06-14
  • 2020-11-01
  • 2015-09-21
  • 2018-02-28
  • 2016-12-13
  • 2016-03-14
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
相关资源
最近更新 更多