2016-07-12 6 views
17

ネットコア1.0.0に 'いいえデータベースプロバイダがこのDbContextのために設定されていない' - SDKプレビュー2(x64)のSignInManager.PasswordSignInAsync

ネットコア1.0.0 - "15" プレビュー2 VS (x64)の

ネットコア1.0.0 - だから、ランタイム(x64)の

、我々は上記の最新バージョンにRC1アプリを更新しました。数時間の参照の切り替えが終わると、その参照は実行されています。

例外InvalidOperationException::いいえデータベース・プロバイダーは、このために設定されていないことは、次のエラーメッセージで吹く

public class AccountController : BaseController 
{ 
    public UserManager<ApplicationUser> UserManager { get; private set; } 
    public SignInManager<ApplicationUser> SignInManager { get; private set; } 
    private readonly IEmailSender EmailSender; 

    public AccountController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IEmailSender emailSender) 
    { 
     UserManager = userManager; 
     SignInManager = signInManager; 
     EmailSender = emailSender; 
    } 

    // GET: /Account/Login 
    [HttpGet] 
    [AllowAnonymous] 
    public IActionResult Login(string returnUrl = null) 
    { 
     ViewBag.ReturnUrl = returnUrl; 
     return View(); 
    } 

    // 
    // POST: /Account/Login 
    [HttpPost] 
    [AllowAnonymous] 
    [ValidateAntiForgeryToken] 
    public async Task<IActionResult> Login(ViewModels.Account.LoginViewModel model, string returnUrl = null) 
    { 
     if (ModelState.IsValid) 
     { 
      // Errs this next line 
      var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false); // <-- ERRS HERE '.PasswordSignInAsync' 
      if (result.Succeeded) 
       return RedirectToLocal(returnUrl); 

      ModelState.AddModelError("", "Invalid email or password."); 
      return View(model); 
     } 

     // If we got this far, something failed, redisplay form 
     return View(model); 
    } 

:(AccountController /ログイン)ログイン時にしかし、私は時のエラーを取得していますDbContext。プロバイダーは、DbContext.OnConfiguringメソッドをオーバーライドするか、アプリケーションサービスプロバイダーでAddDbContextを使用して構成できます。 AddDbContextを使用する場合は、DbContext型がコンストラクタ内のDbContextOptionsオブジェクトを受け入れ、DbContextの基本コンストラクタに渡すようにしてください。ここで

Startup.csです:

public void ConfigureServices(IServiceCollection services) 
    { 
     services.Configure<AppSettings>(Configuration.GetSection("AppSettings")); 

     // Add EF services to the services container. 
     services.AddEntityFrameworkSqlServer() 
      .AddDbContext<LogManagerContext>(options => 
       options.UseSqlServer(Configuration["Data:DefaultConnection:Connectionstring"])); 

     services.AddSingleton(c => Configuration); 

     // Add Identity services to the services container. 
     services.AddIdentity<ApplicationUser, IdentityRole>() 
      .AddEntityFrameworkStores<LogManagerContext>() 
      .AddDefaultTokenProviders(); 


     // Add MVC services to the services container. 
     services.AddMvc(); 

     services.AddTransient<IHttpContextAccessor, HttpContextAccessor>(); 

     //Add all SignalR related services to IoC. - Signal R not ready yet - Chad 
     //services.AddSignalR(); 

     //Add InMemoryCache 
     services.AddMemoryCache(); 

     services.AddSession(options => 
     { 
      options.IdleTimeout = System.TimeSpan.FromHours(1); 
      options.CookieName = ".LogManager"; 
     }); 

     // Uncomment the following line to add Web API servcies which makes it easier to port Web API 2 controllers. 
     // You need to add Microsoft.AspNet.Mvc.WebApiCompatShim package to project.json 
     // services.AddWebApiConventions(); 
     // Register application services. 
     services.AddTransient<IEmailSender, AuthMessageSender>(); 

    } 

    // Configure is called after ConfigureServices is called. 
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
    { 
     app.UseSession(); 

     // Configure the HTTP request pipeline. 
     // Add the console logger. 
     //loggerFactory.MinimumLevel = LogLevel.Information; - moved to appsettings.json -chad 
     loggerFactory.AddConsole(); 
     loggerFactory.AddDebug(); 

     loggerFactory.AddNLog(); 

     // Add the following to the request pipeline only in development environment. 
     if (env.IsDevelopment()) 
     { 
      app.UseBrowserLink(); 
      app.UseDeveloperExceptionPage(); 
      //app.UseDatabaseErrorPage(DatabaseErrorPageOptions.ShowAll); 
     } 
     else 
     { 
      // Add Error handling middleware which catches all application specific errors and 
      // sends the request to the following path or controller action. 
      app.UseExceptionHandler("/Home/Error"); 
     } 

     env.ConfigureNLog("NLog.config"); 

     // Add static files to the request pipeline. 
     app.UseStaticFiles(); 

     // Add cookie-based authentication to the request pipeline. 
     app.UseIdentity(); 

     //SignalR 
     //app.UseSignalR(); 

     // Add MVC to the request pipeline. 
     app.UseMvc(routes => 
     { 
      routes.MapRoute(
      name: "default", 
      template: "{controller}/{action}/{id?}", 
      defaults: new { controller = "Home", action = "Index" } 
      ); 

      // Uncomment the following line to add a route for porting Web API 2 controllers. 
      // routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}"); 
     }); 
    } 

そして、ここでは、コンテキストの:

public class ApplicationUser : IdentityUser 
{ 
    // Add Custom Profile Fields 
    public string Name { get; set; } 
} 

public class LogManagerContext : IdentityDbContext<ApplicationUser> 
{ 
    public DbSet<LogEvent> LogEvents { get; set; } 
    public DbSet<Client> Clients { get; set; } 
    public DbSet<LogEventsHistory> LogEventsHistory { get; set; } 
    public DbSet<LogEventsLineHistory> LogEventsLineHistory { get; set; } 
    public DbSet<LogRallyHistory> LogRallyHistory { get; set; } 
    public DbSet<Flag> Flags { get; set; } 
    protected override void OnModelCreating(ModelBuilder builder) 
    { 

     builder.Entity<LogEvent>().HasKey(x => x.LogId); 
     builder.Entity<LogEvent>().ToTable("LogEvents"); 
     builder.Entity<Client>().HasKey(x => x.ClientId); 
     builder.Entity<Client>().ToTable("Clients"); 
     builder.Entity<LogEventsHistory>().HasKey(x => x.HistoryId); 
     builder.Entity<Flag>().HasKey(x => x.FlagId); 
     builder.Entity<Flag>().ToTable("Flags"); 
     builder.Entity<LogRallyHistory>().HasKey(x => x.HistoryId); 
     builder.Entity<LogEventsLineHistory>().HasKey(x => x.LineHistoryId); 

     base.OnModelCreating(builder); 
    } 

答えて

30

AddDbContextが使用されている場合は、またあなたのDbContextタイプ がDbContextOptionsを受け入れることを確認してくださいDbContextの基本コンストラクタ に渡します。

エラーメッセージがあなたのDbContextLogManagerContext)がDbContextOptionsを受け入れるコンストラクタが必要であると言います。しかし、私はあなたのDbContextでそのようなコンストラクタを見つけることができませんでした。したがって、以下のコンストラクタを追加するとおそらくあなたの問題は解決されます。コメント

ため

public LogManagerContext(DbContextOptions options) : base(options) 
    { 
    } 

を編集しますコードの下に使用して、明示的にIHttpContextAccessorを登録しない場合:私は、接続文字列を追加してあるMyContextでの設定をオーバーライドすることで、それを解決することができ

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
+0

これでLogManagerContext.csにそれを追加しました。これで、より少ない情報で異なるエラーが発生します: InvalidOperationException:HttpContextをnullにすることはできません。 get_Context そして、私はまだ.PasswordSignInAsyncメソッドに入ることができません –

+1

IHttpContextAccessorを登録しようとすることはできますか? https://github.com/aspnet/Mvc/issues/3936 –

+0

services.AddSingleton ();を参照してください。 services.AddSingleton (); これをStartup.csに追加して固定しました。ありがとう。そのリンクを再投稿したい場合は、回答としてマークします。 –

4

DbContextOptionsBuilder:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
    { 
     if (!optionsBuilder.IsConfigured) 
     { 
      IConfigurationRoot configuration = new ConfigurationBuilder() 
       .SetBasePath(Directory.GetCurrentDirectory()) 
       .AddJsonFile("appsettings.json") 
       .Build(); 
      var connectionString = configuration.GetConnectionString("DbCoreConnectionString"); 
      optionsBuilder.UseSqlServer(connectionString); 
     } 
    } 
+0

ニース、私はおそらく最も重要な行を忘れていた: 'optionsBuilder.UseSqlServer(connectionString);' – mkb

関連する問題