我正尝试创建一个新用户和一个角色,并按照以下说明为该用户分配角色
var user = new User
{
EmailAddress = email,
Name = name,
Surname = surName,
UserName = email,
Password = Guid.NewGuid().ToString(),
TenantId = existingTenant.Id,
CreatorUserId = AbpSession.UserId
};
user.SetNormalizedNames();
var existingUserId = _userRepository.InsertOrUpdateAndGetId(user);
existingUser = _userRepository.Get(existingUserId);
if (existingRole == null || existingRole.Id == 0)
{
var newRole = new Role
{
DisplayName = role,
Name = role,
TenantId = existingTenant.Id,
CreatorUserId = AbpSession.UserId
};
newRole.SetNormalizedName();
var modifiedRole = _roleRepository.InsertOrUpdateAndGetId(newRole);
}
await _userManager.AddToRoleAsync(existingUser, role));
CurrentUnitOfWork.SaveChanges();
AddToRoleAsync 始终导致System.ArgumentNullException:值不能为null。参数名称:unitOfWork
我不确定可能出了什么问题。
答案 0 :(得分:0)
我假设由于CurrentUnitOfWork的值而引发了异常。为了跟踪问题,请尝试使用IUnitOfWorkManager。
private readonly IUnitOfWorkManager _unitOfWorkManager;
...
using (var unitOfWork = _unitOfWorkManager.Begin())
{
//your operation
unitOfWork.Complete();
}