【发布时间】: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