.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)
所以,我们更新了RC1应用程序到上面的最新版本.经过多小时的切换参考,它正在运行.但是,登录时(AccountController / Login),我收到错误:
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); }
它会产生以下错误消息:
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.
这是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); }
解决方法
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.
错误消息说您的DbContext(LogManagerContext)需要一个接受DbContextOptions的构造函数.但是我在DbContext中找不到这样的构造函数.所以添加下面的构造函数可能会解决你的问题.
public LogManagerContext(DbContextOptions options) : base(options) { }
编辑评论
如果不明确注册IHttpContextAccessor,请使用下面的代码:
services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>();
原文地址:https://www.jb51.cc/csharp/92522.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。