1

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)

SpruceMoose
  • 9,737
  • 4
  • 39
  • 53
Murat Can OĞUZHAN
  • 735
  • 11
  • 19

1 Answers1

2

I changed codes like that and its work but that is a weird and when i change name that changes automatic IdentityRole normalized name column.

 var role = await _roleManager.FindByIdAsync(IdentityRole.Id);
            role.Name = IdentityRole.Name;
            await _roleManager.UpdateAsync(role);

I found there enter link description here

Murat Can OĞUZHAN
  • 735
  • 11
  • 19