Update Error with Entityframework

Asked

Viewed 7,999 times

1

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

Store update, Insert, or delete statement affected an Unexpected number of Rows (0). Entities may have been modified or Deleted Since entities Were Loaded. See http://go.microsoft.com/fwlink/? Linkid=472540 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 update, insert or delete instruction affected a unexpected number of lines (0). 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 undergone changes through the debug I just don’t understand why EPH can’t detect them.

3 answers

1


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

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

0

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

To solve it, I used:

@Html.HiddenFor(model => model.CampoId).

-3

Store update, Insert, or delete statement affected an Unexpected number of Rows (0). Entities may have been modified or Deleted Since entities Were Loaded. Refresh Objectstatemanager Entries.

  • Welcome to Stack Overflow! Use this language as default on this site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.