Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

ASP.NET Core error : database operation expected to affect 1 row(s) but actually affected 0 row(s)

Ask Question

I have a problem and I tried to solve it a lot, which is when I want to modify the user data by the function EditOrCreateInformation() this error exception message appears:

Microsoft.EntityFrameworkCore.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.

Screenshot of the error is display here

Controller code:

[HttpPost]
public IActionResult EditOrCreateInformation(ApplicationUserVm model, ApplicationUser user)
    if (ModelState.IsValid)
        var olddata = context.Users.Where(a => a.Id == user.Id).AsNoTracking().FirstOrDefault();
        string oldfilename = olddata.PhotoUrl;
        if (model.Photo == null)
            model.PhotoUrl = oldfilename;
        if (oldfilename != null)
            if (model.Photo != null && System.IO.File.Exists(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "PhotoFiles/PhotoProfile", oldfilename)))
                System.IO.File.Delete(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "PhotoFiles/PhotoProfile", oldfilename));
                string PhysicalPath = Path.Combine(Directory.GetCurrentDirectory() + "/wwwroot", "PhotoFiles/PhotoProfile/");
                // 2) Get File Name
                string FileName = Guid.NewGuid() + Path.GetFileName(model.Photo.FileName);
                // 3) Merge Physical Path + File Name
                string FinalPath = Path.Combine(PhysicalPath, FileName);
                // 4) Save The File As Streams "Data Over Time"
                using(var stream = new FileStream(FinalPath, FileMode.Create))
                    model.Photo.CopyTo(stream);
                model.PhotoUrl = FileName;
            string PhysicalPath = Path.Combine(Directory.GetCurrentDirectory() + "/wwwroot", "PhotoFiles/PhotoProfile/");
            // 2) Get File Name
            string FileName = Guid.NewGuid() + Path.GetFileName(model.Photo.FileName);
            // 3) Merge Physical Path + File Name
            string FinalPath = Path.Combine(PhysicalPath, FileName);
            // 4) Save The File As Streams "Data Over Time"
            using(var stream = new FileStream(FinalPath, FileMode.Create))
                model.Photo.CopyTo(stream);
            model.PhotoUrl = FileName;
        var obj = mapper.Map < ApplicationUser > (model);
        applicationUser.Update(obj);
        toastNotification.AddSuccessToastMessage("Your Information Updated successfully");
        return RedirectToAction("MyProfile", "Profile", new
            Area = "Identity"
    return View(model);

Repository code:

public ApplicationUser Update(ApplicationUser obj)
    db.Entry(obj).State = EntityState.Modified;
    db.SaveChanges();
    return db.Users.Where(a => a.Id == obj.Id).FirstOrDefault();

How can I fix this?

I will be very grateful to help me solve this problem.

Because the sql update where clause, which includes all PK and concurrency columns, did not impact any rows. stackoverflow.com/questions/39460686/… – Jeremy Lakeman Jun 20, 2022 at 4:48 it's because the object you want to update is not the object that came from the database, so the first and the simplest way is to modify oldData instead the model – Parsa S Jun 20, 2022 at 6:34
  • using different DbContext
  • First, The AsNoTracking should be only used on read-only queries aka SELECT. If you need to UPDATE the same object, then you should select the object without using AsNoTracking.

    Secondly, Update method you pass the modified User object to a different context that does not have the current object's change tracking history. So, when SaveChanges executed, it won't see any changes; because the object's changes were made in a different context.

    so what you need to do is just remove AsNoTracking and replace these lines :

    var obj = mapper.Map < ApplicationUser > (model);
    applicationUser.Update(obj);
    

    with this :

    olddata.PhotoUrl = model.PhotoUrl;
    context.Entry(olddata).State = EntityState.Modified;
    db.SaveChanges();
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.