I am trying update exist role but i am getting error.
Code
private readonly RoleManager<IdentityRole> _roleManager;
public EditModel(RoleManager<IdentityRole> roleManager)
{
_roleManager = roleManager;
}
[BindProperty]
public IdentityRole IdentityRole { get; set; }
public async Task<IActionResult> OnGetAsync(string id)
{
if (id == null)
{
return NotFound();
}
IdentityRole = await _roleManager.FindByIdAsync(id);
if (IdentityRole == null)
{
return NotFound();
}
return Page();
}
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return Page();
}
try
{
await _roleManager.UpdateAsync(IdentityRole);//Error is occuring here.
}
catch (DbUpdateConcurrencyException)
{
}
return RedirectToPage("./Index");
}
Error
InvalidOperationException: The instance of entity type 'IdentityRole' 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. Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.Add(TKey key, InternalEntityEntry entry)