我试图更新我的UserRoles表,但它不会更新。我试图更新两件事:1.电子邮件2.用户角色。因为更新需要在两个表中进行,所以我使用了两个单独的命令。当我单独运行电子邮件更新(用户)时,它的工作原理,但如果我更新角色(AspUserRoles),它什么都不做。当我运行它时,它都不起作用,因为UserRoles.Attach(userRole)阻止它更新。我也没有错误。无法更新。安装()
我检查了ApplicationRole.Id和ApplicationUser.Id是否有值,它确实返回了我想要的值。
这里是我的UserController.cs:
public async Task<IActionResult> Edit(UserViewModel model, Guid id)
{
var alert = new Alert();
try
{
if(!ModelState.IsValid)
{
alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.INVALID;
throw new Exception();
}
var originalModel = ApplicationDbContext.Users.FirstOrDefault(u => u.Id == id);
var userRole = ApplicationDbContext.UserRoles.FirstOrDefault(i => i.UserId == id);
if(originalModel == null)
{
alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.NOTEXISTS;
throw new Exception();
}
originalModel.Email = model.ApplicationUser.Email;
userRole.RoleId = model.ApplicationRole.Id;
ApplicationDbContext.Users.Attach(originalModel);
ApplicationDbContext.UserRoles.Attach(userRole);
ApplicationDbContext.Entry(originalModel).State = EntityState.Modified;
if (await ApplicationDbContext.SaveChangesAsync() == 0)
{
alert.Message = alert.ExceptionMessage = ApplicationDbContextMessage.EDITNOK;
throw new Exception();
}
alert.Message = ApplicationDbContextMessage.EDITOK;
return RedirectToAction("Index");
}
catch(Exception ex)
{
alert.Type = AlertType.Error;
alert.ExceptionMessage = ex.Message;
model = await ViewModel(model.ApplicationUser);
ModelState.AddModelError(string.Empty, alert.ExceptionMessage);
}
return View(model);
}
您不应该在这些对象上调用'Attach',它们来自它们保存的相同上下文,并且它们永远不会断开连接。你是否尝试过这种方式,而不调用'Attach'或者搞乱'EntityState'? –
我做了,它不起作用。当我做Add(userRole)而不是Attach(userRole)时,它确实在UserRole表中添加了一个新行,但是我有一个具有相同UserId的dublicate ... – hxwtch
你不需要调用'Add'。只需更改属性,并在上下文中调用“SaveChanges”。你应该能够拿到你原来的发布代码,删除2个表示'Attach'的行,以及改变'State'的行,并让它工作。 –