Error in Update with EntityFramework

1

When I try to update an object in the database, I get the error,

  

Store update, insert, or delete statement   number of rows (0). Entities may have been modified or deleted since   groups were loaded. See    link for information on   understanding and handling optimistic concurrency exceptions.

In this code:

[HttpPost]
public async Task<ActionResult> Editar(Cliente cliente)
{
  if (cliente.Titulo != null)
  {
    db.Entry(cliente.Titulo).State = EntityState.Modified;
    db.SaveChanges();
  }
}

A translation for the error, is:

  

Store upgrade, insertion, or deletion statement affected a   unexpected number of rows (0). The entities may have been   modified, or deleted since entities were loaded.

I want to know, what does this error mean and why is it occurring in my code? I made sure that the object has been changed by debug I just do not understand why EF is not able to detect them.

    
asked by anonymous 10.05.2017 / 02:28

2 answers

1

The error means that cliente.Titulo does not yet exist in database, so the Entity Framework returns a message saying that there are no objects with the Id passed in the object.

Possibly the Id is coming down from the screen or comes a value that does not yet exist as a key.

    
10.05.2017 / 02:39
0

In my case (AspNet MVC5) the problem happened because when loading the registry for change, I did not load the key field ( Id ) that was not exposed to the user.

To solve, I used:

@Html.HiddenFor(model => model.CampoId).
    
07.08.2018 / 14:45