使用 ASP .NET Core Identity 和 EntityFrameworkCore 注册新用户时出现 InvalidOperationException
InvalidOperationException when registering a new user with ASP .NET Core Identity and EntityFrameworkCore
我正在关注 documentation for using Identity 并正在尝试注册一个新用户(执行注册操作),但失败并出现以下错误:
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:
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
创建并应用了新的迁移
我发现了问题。我的 ApplicationContext
继承自 DbContext
。我将其更改为 IdentityDbContext<ApplicationUser>
并且有效。
创建继承 IdentityDbContext 的新上下文 class。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
并在 startup.cs 文件中添加以下代码
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connection));
这将有助于您使用数据库优先方法。
我正在关注 documentation for using Identity 并正在尝试注册一个新用户(执行注册操作),但失败并出现以下错误:
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:
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
创建并应用了新的迁移
我发现了问题。我的 ApplicationContext
继承自 DbContext
。我将其更改为 IdentityDbContext<ApplicationUser>
并且有效。
创建继承 IdentityDbContext 的新上下文 class。
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
}
}
并在 startup.cs 文件中添加以下代码
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connection));
这将有助于您使用数据库优先方法。