its0mar
its0mar
CC#
Created by its0mar on 1/29/2025 in #help
Optimistic concurrency failure, object has been modified
public async Task<IActionResult> UpdateInfo(UpdateInfoDTO dto)
{
if (!ModelState.IsValid)
{
ViewBag.Errors = ModelState.Values.SelectMany(e => e.Errors).Select(e => e.ErrorMessage).ToList();
return View(dto);
}

ApplicationUser user = new ApplicationUser { UserName = dto.UserName, PersonName = dto.PersonName, Email = dto.Email, PhoneNumber = dto.Phone, ProfilePicPath = dto.ProfilePicture };
user.SecurityStamp = Guid.NewGuid().ToString();
var result = await _userManager.UpdateAsync(user);

if (result.Succeeded)
{
return RedirectToAction(nameof(HomeController.Index), "Home");
}
else
{
foreach (IdentityError error in result.Errors)
{
ModelState.AddModelError("UpdateInfo", error.Description);
}
return View(dto);
}

}
public async Task<IActionResult> UpdateInfo(UpdateInfoDTO dto)
{
if (!ModelState.IsValid)
{
ViewBag.Errors = ModelState.Values.SelectMany(e => e.Errors).Select(e => e.ErrorMessage).ToList();
return View(dto);
}

ApplicationUser user = new ApplicationUser { UserName = dto.UserName, PersonName = dto.PersonName, Email = dto.Email, PhoneNumber = dto.Phone, ProfilePicPath = dto.ProfilePicture };
user.SecurityStamp = Guid.NewGuid().ToString();
var result = await _userManager.UpdateAsync(user);

if (result.Succeeded)
{
return RedirectToAction(nameof(HomeController.Index), "Home");
}
else
{
foreach (IdentityError error in result.Errors)
{
ModelState.AddModelError("UpdateInfo", error.Description);
}
return View(dto);
}

}
hello, when I try to update a user I get Optimistic concurrency failure, object has been modified , how I can fix it and what is the reason of it
8 replies