2016-07-12 5 views
17
पर 'कोई डेटाबेस प्रदाता इस DbContext के लिए कॉन्फ़िगर किया गया है'

नेट कोर 1.0.0 - एसडीके पूर्वावलोकन 2 (64)SignInManager.PasswordSignInAsync

नेट कोर 1.0.0 - वी.एस. "15" पूर्वावलोकन 2 (64)

नेट कोर 1.0.0 - क्रम (64)

तो, हम ऊपर नवीनतम संस्करण के लिए एक RC1 ऐप को अपडेट किया। स्विचिंग संदर्भों के कई घंटों के बाद, यह चल रहा है।

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/लॉगिन), मैं एक त्रुटि पर हो रही है

InvalidOperationException: नहीं डेटाबेस प्रदाता इस बात के लिए कॉन्फ़िगर किया गया है DbContext। एक प्रदाता को DbContext.OnConfiguring विधि को ओवरराइड करके या अनुप्रयोग सेवा प्रदाता पर AddDbContext का उपयोग करके कॉन्फ़िगर किया जा सकता है। यदि AddDbContext का उपयोग किया जाता है, तो यह भी सुनिश्चित करें कि आपका डीबीकॉन्टेक्स्ट प्रकार अपने कन्स्ट्रक्टर में डीबीकॉन्टेक्स्ट ऑप्शन ऑब्जेक्ट स्वीकार करता है और इसे डीबीकॉन्टेक्स्ट के लिए बेस कन्स्ट्रक्टर में भेज देता है। यहाँ

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?}"); 
     }); 
    } 

और संदर्भ दिया गया है::

यहाँ Startup.cs है

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 (LogManagerContext) एक निर्माता है जो एक DbContextOptions स्वीकार करता है की जरूरत है। लेकिन मुझे आपके DbContext में ऐसा कोई कन्स्ट्रक्टर नहीं मिला। तो नीचे कन्स्ट्रक्टर जोड़ना शायद आपकी समस्या हल करता है।

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

टिप्पणी

के लिए संपादित करें आप IHttpContextAccessor स्पष्ट रूप से रजिस्टर नहीं है, तो नीचे दिए गए कोड का उपयोग करें:

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

ठीक है, इसलिए मैंने इसे LogManagerContext.cs में जोड़ा और अब मुझे कम जानकारी के साथ एक अलग त्रुटि मिलती है: अमान्य ऑपरेशन अपवाद: HttpContext शून्य नहीं होना चाहिए। get_Context और मैं अभी भी .PasswordSignInAsync विधि में कदम नहीं उठा सकता –

+1

क्या आप IHttpContextAccessor को पंजीकृत करने का प्रयास कर सकते हैं? https://github.com/aspnet/Mvc/issues/3936 –

+0

सेवाओं को देखें। AddSingleton () जोड़ें; सेवाएं। AddSingleton (); इसे Startup.cs में जोड़कर इसे ठीक किया गया। धन्यवाद।अगर आप उस लिंक को दोबारा पोस्ट करना चाहते हैं तो मैं इसे उत्तर के रूप में चिह्नित करूंगा। –

4

मैं में जोड़ने से कनेक्शन स्ट्रिंग के माध्यम से MyContext में विन्यास अधिभावी द्वारा इसे हल कर सकता है डीबीकॉन्टेक्स्टऑप्शन बिल्डर:

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

अच्छा, मैं शायद सबसे महत्वपूर्ण पंक्ति भूल गया: 'विकल्पबिल्डर। यूएसएसक्लसे सर्वर (कनेक्शनस्ट्रिंग);' – mkb

संबंधित मुद्दे