我正在关注使用身份的文档并尝试注册一个新用户(执行注册操作),但失败并出现以下错误:
InvalidOperationException:无法为“ApplicationUser”创建 DbSet
因为该类型不包含在上下文的模型中。
Startup:
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);
}
我已经尝试过以下操作:
- Adding
DbSet<ApplicationUser>
to AplicationContext
- 我确实通过以下方式创建并应用了新的迁移
dotnet ef
我发现了问题。我的ApplicationContext
继承自DbContext
。我把它改为IdentityDbContext<ApplicationUser>
它有效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)