i have table for set user Access Level in Tabel .
this is my Access
:
public Guid RoleId { get; set ; }
public string Access { get ; set ; }
i want when the AccessLevel
is changed it must changed the SecurityStamp
in Role
table .
public async Task<OperationResult<string>> Handle(SetAccessLevelCommand request, CancellationToken cancellationToken)
{
var result = await unitOfWork.RoleRepository.AccessLevelRepository.SetAccess(new AccessLevelDto { RoleId = request.RoleId, Access = request.AccessList });
if (result.Success)
{
try
{
try
{
var findRole = await unitOfWork.RoleRepository.GetRoleByIdAsync(request.RoleId, cancellationToken);
findRole.Result.UpdateSecurityStamp();
if (findRole.Result != null)
{
unitOfWork.RoleRepository.Update(findRole.Result, cancellationToken);
unitOfWork.CommitSaveChange();
return OperationResult<string>.BuildSuccessResult("Add Success");
}
}
catch (Exception ex)
{
return OperationResult<string>.BuildFailure(ex.Message);
}
}
catch (Exception ex)
{
return OperationResult<string>.BuildFailure(ex.Message);
}
}
return OperationResult<string>.BuildFailure(result.ErrorMessage);
}
i write this code for doing this work .
this is the SetAccess
:
public async Task<OperationResult<string>> SetAccess(AccessLevelDto accessLevels)
{
try
{
var currentRoleAccessValue = GetAccessLevels(accessLevels.RoleId);
var currentAccess = currentRoleAccessValue.Select(x => x.Access).ToList();
var newAccess = accessLevels.Access.Except(currentAccess).ToList();
if (newAccess != null)
{
foreach (var item in newAccess)
{
context.Add(new AccessLevel
{
Access = item,
RoleId = accessLevels.RoleId
});
}
}
var removeItems = currentAccess.Except(accessLevels.Access).ToList();
if (removeItems != null)
{
foreach (var item in removeItems)
{
var accClaim = currentRoleAccessValue.SingleOrDefault(x => x.Access == item);
if (accClaim != null)
{
context.Remove(accClaim);
}
}
}
return OperationResult<string>.BuildSuccessResult("SuccessAdd");
}
catch (Exception ex)
{
return OperationResult<string>.BuildFailure(ex);
}
}
this is code for change State
of entity and it's worked fine and when i call the CommitSaveChange()
it worked fine . but when i add the RoleUpdate
commands :
var findRole = await unitOfWork.RoleRepository.GetRoleByIdAsync(request.RoleId, cancellationToken);
findRole.Result.UpdateSecurityStamp();
if (findRole.Result != null)
{
unitOfWork.RoleRepository.Update(findRole.Result, cancellationToken);
}
and then call the unitOfWork.CommitSaveChange()
it show me this error :
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances. Object name: 'FilmstanContext'.
whats the problem ? how can i solve this problem ????