【发布时间】:2020-04-01 17:20:27
【问题描述】:
我创建了一个带有数据库的 asp.net MVC 网站。我最近添加了一个身份数据库。在我的机器上一切正常。但是,当我在另一台计算机上运行它时,它给了我这个错误:
System.Data.SqlClient.SqlException: 'Invalid object name 'AspNetUsers'.'
这是我的 AccountController
public class AccountController : Controller
{
private UserManager<IdentityUser> userManager;
private SignInManager<IdentityUser> signInManager;
public AccountController(UserManager<IdentityUser> userMgr,
SignInManager<IdentityUser> signInMgr)
{
userManager = userMgr;
signInManager = signInMgr;
IdentitySeedData.EnsurePopulated(userMgr).Wait();
}
[AllowAnonymous]
public ViewResult Login(string returnUrl)
{
return View(new LoginModel
{
ReturnUrl = returnUrl
});
}
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel loginModel)
{
if (ModelState.IsValid)
{
IdentityUser user =
await userManager.FindByNameAsync(loginModel.Name);
if (user != null)
{
await signInManager.SignOutAsync();
if ((await signInManager.PasswordSignInAsync(user,
loginModel.Password, false, false)).Succeeded)
{
return Redirect(loginModel?.ReturnUrl ?? "/Admin/Index");
}
}
}
ModelState.AddModelError("", "Invalid name or password");
return View(loginModel);
}
public async Task<RedirectResult> Logout(string returnUrl = "/")
{
await signInManager.SignOutAsync();
return Redirect(returnUrl);
}
}
这是我的 IdentitySeedData
public class IdentitySeedData
{
private const string adminUser = "Admin";
private const string adminPassword = "Secret123$";
public static async Task EnsurePopulated(UserManager<IdentityUser> userManager)
{
IdentityUser user = await userManager.FindByIdAsync(adminUser);
if (user == null)
{
user = new IdentityUser("Admin");
await userManager.CreateAsync(user, adminPassword);
}
}
}
在另一台计算机上运行时的错误总是在这里生成:
public AccountController(UserManager<IdentityUser> userMgr,
SignInManager<IdentityUser> signInMgr)
{
userManager = userMgr;
signInManager = signInMgr;
IdentitySeedData.EnsurePopulated(userMgr).Wait();
}
项目链接:
https://github.com/DemarioDouce/SoccerClub
我很想知道如何解决这个错误。
【问题讨论】:
标签: c# asp.net asp.net-mvc model-view-controller