【问题标题】:Aspnet Core Identity Role Update ErrorAspnet Core 身份角色更新错误
【发布时间】:2018-05-08 17:45:09
【问题描述】:

我正在尝试更新现有角色,但出现错误。

代码

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");
    }

错误

InvalidOperationException:无法跟踪实体类型“IdentityRole”的实例,因为已经在跟踪具有相同键值 {'Id'} 的另一个实例。附加现有实体时,请确保仅附加一个具有给定键值的实体实例。考虑使用“DbContextOptionsBuilder.EnableSensitiveDataLogging”来查看冲突的键值。 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.Add(TKey key, InternalEntityEntry entry)

【问题讨论】:

    标签: .net-core asp.net-identity


    【解决方案1】:

    我更改了这样的代码及其工作,但这很奇怪,当我更改名称时会更改自动 IdentityRole 规范化名称列。

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

    我在那里找到enter link description here

    【讨论】:

      猜你喜欢
      • 2017-11-03
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2014-07-22
      • 2020-12-01
      • 1970-01-01
      • 2015-04-06
      相关资源
      最近更新 更多