小编use*_*564的帖子

无法访问已处置的对象。\r\n对象名称:“UserManager`1 - 调用 CreateAsync(user, model.password) 时

我正在开发 .net core 项目。我的项目结构有 4 个项目。

  1. Student_Database -(包含数据库表模型和ApplicatinDBContext)
  2. Student_Entities -(包含所有视图侧模型)
  3. Student_Service - (所有数据库操作都从这里处理。它连接到数据库。例如:IUserService 和 UserService)
  4. Student_Web -(控制器和所有方法、逻辑以及所有视图都在这个项目中)

我已经实现了实体框架核心。并尝试使用 Usermanager 来插入数据。现在,当我从控制器(Student_Web)调用方法“CreateAsync”时,它工作正常并且用户插入。但我想在Student_Service中实现数据库操作。因此,当我从 UserService 调用“CreateAsync”时,它会给出错误“无法访问已处置的对象。\r\n对象名称:'UserManager`1”

我从控制器调用此接口 IUserService。这是我在 UserService 中的代码。

请帮我解决这个问题。

public class UserService : IUserService
{
    #region Properties

    private readonly IDbContext _context;
    private readonly UserManager<ApplicationUser> _userManager;
    private readonly RoleManager<IdentityRole<int>> _roleManager;

    #endregion

    #region Consturctor

    public UserService(
        IDbContext context
        , UserManager<ApplicationUser> userManager
        , RoleManager<IdentityRole<int>> roleManager
    {
        _context = context;
        _userManager = userManager;
        _roleManager = roleManager;
    }
    #endregion

    #region Methods
        
    public async Task<bool> Create(NewUsers model)
    { …
Run Code Online (Sandbox Code Playgroud)

c# claims-based-identity asp.net-identity asp.net-core ef-core-3.1

5
推荐指数
1
解决办法
2962
查看次数