'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync

asp.net-core asp.net-identity-3 c# entity-framework


.Net Core 1.0.0 - SDK Preview 2 (x64)

.Net Core 1.0.0 - VS "15" Preview 2 (x64)

.Net Core 1.0.0 - Runtime (x64)

So, we updated an RC1 app to the latest versions above. After many hours of switching references, it's running. However, when logging in (AccountController/Login), I am getting an error at:

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
    public IActionResult Login(string returnUrl = null)
        ViewBag.ReturnUrl = returnUrl;
        return View();

    // POST: /Account/Login
    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);

It blows up with the following error message:

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. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

Here is the Startup.cs:

public void ConfigureServices(IServiceCollection services)

        // Add EF services to the services container.
           .AddDbContext<LogManagerContext>(options =>

        services.AddSingleton(c => Configuration);

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

        // Add MVC services to the services container.

        services.AddTransient<IHttpContextAccessor, HttpContextAccessor>();

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

        //Add InMemoryCache

        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)

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


        // Add the following to the request pipeline only in development environment.
        if (env.IsDevelopment())
            // Add Error handling middleware which catches all application specific errors and
            // sends the request to the following path or controller action.


        // Add static files to the request pipeline.

        // Add cookie-based authentication to the request pipeline.


        // Add MVC to the request pipeline.
        app.UseMvc(routes =>
             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?}");

And here's the Context:

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<Client>().HasKey(x => x.ClientId);
        builder.Entity<LogEventsHistory>().HasKey(x => x.HistoryId);
        builder.Entity<Flag>().HasKey(x => x.FlagId);
        builder.Entity<LogRallyHistory>().HasKey(x => x.HistoryId);
        builder.Entity<LogEventsLineHistory>().HasKey(x => x.LineHistoryId);

7/12/2016 10:26:14 PM

Accepted Answer

If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions object in its constructor and passes it to the base constructor for DbContext.

The error message says your DbContext(LogManagerContext ) needs a constructor which accepts a DbContextOptions. But i couldn't find such a constructor in your DbContext. So adding below constructor probably solves your problem.

    public LogManagerContext(DbContextOptions options) : base(options)

Edit for comment

If you don't register IHttpContextAccessor explicitly, use below code:

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); 
7/13/2016 8:32:39 PM

Popular Answer

I could resolve it by overriding Configuration in MyContext through adding connection string to the DbContextOptionsBuilder:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        if (!optionsBuilder.IsConfigured)
            IConfigurationRoot configuration = new ConfigurationBuilder()
            var connectionString = configuration.GetConnectionString("DbCoreConnectionString");

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow