I Use Microsoft.AspNetCore.Identity.EntityFrameworkCore in my project
When I try to update the user role I get the following error
InvalidOperationException: The instance of entity type ‘RoleEntity’
cannot be tracked because another instance with the same key value for
{‘Id’} is already being tracked. When attaching existing entities,
ensure that only one entity instance with a given key value is
attached. Consider using
‘DbContextOptionsBuilder.EnableSensitiveDataLogging’ to see the
conflicting key values.
My Code :
public async Task<Result> UpdateRoleAsync(UpdateRoleCommand command, CancellationToken cancellation)
{
var model = await _role.Roles.AsNoTracking().SingleOrDefaultAsync(s => s.Id == command.Id, cancellation);
if(model== null)
{
//To Do
return Result.Fail(FailMessage.Internal);
}
var config = new TypeAdapterConfig();
config.NewConfig<RoleEntity, UpdateRoleCommand>()
.Map(a => a.Name, b => b.Name)
.Map(a => a.PresianName, b => b.PresianName)
.Compile();
model=command.Adapt<RoleEntity>(config);
var result=await _role.UpdateAsync(model);
if (result.Succeeded)
{
return Result.Success();
}
string error = string.Empty;
foreach(var item in result.Errors)
{
error += item.Description + "n";
}
return Result.Fail(error);
}