我正在遵循有关使用身份的文档并且正在尝试注册新用户(执行register操作),但是失败并出现以下错误:
I'm following the documentation for using Identity and am trying register a new user (executing the register action), but it fails with the following error:
InvalidOperationException:无法创建DbSet for'ApplicationUser',因为此类型未包含在上下文模型中。
InvalidOperationException: Cannot create a DbSet for 'ApplicationUser' because this type is not included in the model for the context.
启动:
services.AddIdentity<ApplicationUser, IdentityRole>(options => { //password options options.Password.RequireDigit = false; // ... })我正在使用标准的ApplicationUser:
I am using a standard ApplicationUser:
public class ApplicationUser : IdentityUser { }在AccountController中注册操作:
Register action in 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); }我已经尝试过以下操作:
I already tried the following:
- 将 DbSet< ApplicationUser> 添加到 AplicationContext
- 我确实通过 dotnet ef
- Adding DbSet<ApplicationUser> to AplicationContext
- I did create and apply a new migration via dotnet ef
我发现了问题。我的 ApplicationContext 是从 DbContext 继承的。我将其更改为 IdentityDbContext< ApplicationUser> ,它可以正常工作。
I found the problem. My ApplicationContext was inheriting from DbContext. I changed it to IdentityDbContext<ApplicationUser> and it works.
更多推荐
使用ASP .NET Core身份和EntityFrameworkCore注册新用户时出现InvalidOperationException
发布评论