1

我在我的.NET Core應用程序中使用OpenIddict進行JWT令牌認證。我已經按照this tutorial但現在我收到以下錯誤:爲OpenIddict配置DbContext

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

ConfigureServices方法Startup.cs

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

不知道該怎麼辦關於這個問題,我不能添加的DbContext時使用AddIdentity

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

UPDATE 這是我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); 
} 
+0

能否請您分享您的DB背景?我想確保連接字符串正確流向EntityFramework。 – Pinpoint

+0

我的連接字符串不在我的dbcontext中,它位於我的appsettings.json中 –

+0

您的連接字符串的配置位置並不重要。我只是想確保你的上下文有正確的構造函數來允許外部配置。 – Pinpoint

回答

2

由於的EntityFramework核心RC2設計變更,現在您需要手動流動DbContextOptions或直接在OnModelCreating中配置連接字符串。

嘗試增加此構造你的數據庫環境(應該從OpenIddictContext派生):

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

謝謝你的朋友,我會很快檢查這個問題 –

+0

問題我與這是ApplicationDbContext沒有定義,不知道如何繼續執行此。 –

+0

糟糕,複製/粘貼錯誤。當然,你必須將它命名爲你的上下文類,因爲它是一個構造函數。對不起。 – Pinpoint