0

Using .NET6 and EF Core I'm trying to update a user's details in the database and receiving an error:

DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s);

I'm using a class called AppUser which inherits from IdentityUser.

My update method looks like this:

public async Task<IActionResult> UpdateAccountAsync(AppUser user)
{
    if (!ModelState.IsValid) return View(nameof(MyAccount), user);
    
    user.ModifiedDateT = DateTime.UtcNow;
    
    await _userRepository.UpdateAsync(user);

    return View(nameof(MyAccount), user);
}

With the UpdateAsync method like:

public async Task<AppUser> UpdateAsync(AppUser entity)
{
    if (entity == null) 
        throw new InvalidOperationException("Cannot update a null object!");

    entity.ModifiedDateT = DateTime.UtcNow;

    var dbEntity = _context.Update(entity);

    await _context.SaveChangesAsync();

    return dbEntity.Entity;
}

Using this interface:

Task<AppUser> UpdateAsync(AppUser entity);

With my limited understanding of EF Core, do you have to bring in the AppUser, make changes to the AppUser, then update and save? It's not working out for me

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [Unable to edit db entries using EFCore, EntityState.Modified: "Database operation expected to affect 1 row(s) but actually affected 0 row(s)."](https://stackoverflow.com/questions/39460686/unable-to-edit-db-entries-using-efcore-entitystate-modified-database-operatio) – Progman Oct 11 '22 at 14:43
  • Unfortunately no, that thread is in regards to a different Id being passed in or removing Attach from the object. I have Update and SaveAsync only.. I tried removing .Update() but it doesn't add the changes to the DB and the Id's are matching – Developing Developer Oct 11 '22 at 14:52
  • Hi @marc_s, I included the error at the top of the page. it's not updating, when I look online at other threads for this it says that .SaveChangesAsync creates an update so I've removed the update method which doesn't update the DB with the new properties – Developing Developer Oct 11 '22 at 15:00

1 Answers1

1

I figured it out in the end with:

public async Task<AppUser > UpdateAsync(AppUser entity)
    {
        if (entity == null) throw new InvalidOperationException("Cannot update a null object!");
        entity.ModifiedDateT = DateTime.UtcNow;

        var dbUser = _context.Users
            .FirstOrDefault(s => s.Id.Equals(entity.Id));

        if (dbUser != null)
        {
            dbUser.FirstName = entity.FirstName;
            dbUser.LastName = entity.LastName;
            dbUser.Email = entity.Email;
            dbUser.ModifiedDateT = DateTime.UtcNow;
        }

        await _context.SaveChangesAsync();
        
        return entity;
    }

I didn't realize that the property being passed in won't update, but I needed to get a new property from the DB and then apply the changes to that property and SaveChangesAsync().