I have this error when update record in RoleRepository.cs.
i use AsNoTracking but show error again
The instance of entity type 'ApplicationRole' 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.
RoleController.cs
public async Task<IActionResult> Edit(string id)
{
if (id == null)
{
return NotFound();
}
if (!await _roleService.ExsistIdAsync(id))
{
return NotFound();
}
return PartialView(await _roleService.GetAsync(id));
}
public async Task<IActionResult> Edit(RoleViewModel role)
{
if (await _roleService.ExsistNameAsync(role))
{
//show message duplicate name
}
await _roleService.UpdateAsync(role);
return Json(new { res = true, model = role });
}
RoleService.cs
public async Task<IdentityResult> UpdateAsync(RoleViewModel role)
{
var mapped = ObjectMapper.Mapper.Map<ApplicationRole>(role);
return await _roleRepository.UpdateAsync(mapped);
}
RoleRepository.cs
public async Task<IdentityResult> UpdateAsync(ApplicationRole role)
{
return await _roleManager.UpdateAsync(role);
}
public async Task<ApplicationRole> GetAsync(string Id)
{
return await _db.Roles.Where(x=>x.Id==Id).AsNoTracking().FirstOrDefaultAsync();
}
what's the problem ?