加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用ASP .NET Core Identity和EntityFrameworkCore注册新

发布时间:2020-12-15 06:17:36 所属栏目:百科 来源:网络整理
导读:我正在关注 documentation for using Identity并尝试注册一个新用户(执行注册操作),但它失败并出现以下错误: InvalidOperationException: Cannot create a DbSet for ‘ApplicationUser’ because this type is not included in the model for the context.
我正在关注 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>它的工作原理.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读