.Net核心1.0.0 - SDK预览2 (x64) .Net核心1.0.0 - VS "15“预览2 (x64) .Net核心1.0.0 -运行时(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:没有为这个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);
}发布于 2016-07-13 05:47:21
如果使用AddDbContext,那么还要确保DbContext类型在构造函数中接受DbContextOptions对象,并将其传递给DbContext的基本构造函数。
错误消息说您的DbContext(LogManagerContext )需要一个接受DbContextOptions的构造函数。但我在您的DbContext中找不到这样的构造函数。因此,添加以下构造函数可能解决了您的问题。
public LogManagerContext(DbContextOptions options) : base(options)
{
}编辑评论
如果没有显式注册IHttpContextAccessor,请使用以下代码:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>(); https://stackoverflow.com/questions/38338475
复制相似问题