我正在关注使用 Identity 的文档并尝试注册新用户(执行注册操作),但它失败并出现以下错误:
InvalidOperationException:无法为“ApplicationUser”创建 DbSet,因为此类型未包含在上下文模型中。
启动:
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
//password options
options.Password.RequireDigit = false;
// ...
})
我正在使用标准的 ApplicationUser:
public class ApplicationUser : IdentityUser
{
}
在 AccountController 中注册操作:
public async Task<IActionResult> Register(RegisterViewModel viewModel)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = viewModel.UserName, Email = viewModel.Email };
var result = await _userManager.CreateAsync(user, viewModel.Password); //<-- Exception happens here
if (result.Succeeded)
{
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToAction(nameof(HomeController.Index), "Home");
}
string errorData = "";
foreach (var error in result.Errors)
{
errorData += error.Description + '\n';
}
StoreErrorMessage("Failed to create the user!", errorData);
}
return View(viewModel);
}
我已经尝试了以下方法:
- 添加
DbSet<ApplicationUser>到AplicationContext - 我确实通过
dotnet ef