DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s).

Yalçın Mete 60 Reputation points
2024-07-01T13:44:43.65+00:00

Hi,

If you allow, I would like to ask a question.

I want to replace the data I brought with the "public async Task<IActionResult> WriterEditProfile()" method with the "public IActionResult WriterEditProfile(AppUser p)" method.

"DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded." I get an error.

Why does this occur? Is it because one method is "async" and the other method is not "async" that I get this error?


public class WriterController : Controller{

private readonly UserManager<AppUser> _userManager;

[HttpGet]
public async Task<IActionResult> WriterEditProfile()
{
    var values = await _userManager.FindByNameAsync(User.Identity.Name);
    return View(values);
}
}
[HttpPost]
public IActionResult WriterEditProfile(AppUser p)
{
    UserManager userManager = new UserManager(new EfUserRepository());
    userManager.TUpdate(p);
    return RedirectToAction("Index","Dashboard");
}
public class UserManager : IUserService
{
	IUserDal _userDal;
	public void TUpdate(AppUser t)
	{
		_userDal.Update(t);
	}
}
public class EfUserRepository : GenericRepository<AppUser> , IUserDal
{
}
public class GenericRepository<T> : IGenericDal<T> where T : class
{
public void Update(T t)
{
    using var c = new Context();
    c.Update(t);
    c.SaveChanges();
}

Thanks.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,366 questions
{count} votes