Labels

slider

Recent

Navigation

Entity Framework: Store update insert or delete statement affected an unexpected number of rows (0)

store update insert or delete statement affected an unexpected number of rows (0), store update insert or delete statement affected an unexpected number of rows mvc, store update insert or delete statement affected an unexpected number of rows (0). entities
Introduction I have already explained very known an entity framework error Validation failed for one or more entities, today providing a resolution of another entity framework error. Yesterday, I was working on MVC application using Entity Framework, meanwhile I was setting up my code then a new error comes off me "Store update, insert, or delete statement affected an unexpected number of rows". After my deep study, then came up a perfect solution to remove this issue permanently from my solution. I have explained all steps as given below.

Error Description

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.

Store update insert or delete statement affected an unexpected number of rows (0)
Fix Entity Framework Error

Steps to Remove Error [Store update, insert, or delete statement affected an unexpected]

Set update mode to add, it was earlier set to modified state using entity framework. If table primary key greater than zero then it will modified otherwise it will add a new record in database table.

Wrong Statement

I had written given below wrong statement to add a new fresh record into my database table record, it was wrongly added state to Modified.
_context.Entry(model).State = EntityState.Modified;

Corrected Statement

Now, I corrected this statement, I just checked that primary key contains a value means a record need to update otherwise primary key value 0 means my database table needed to add a new fresh record in to my database table record.

if (model.TablePK > 0)
{
   _context.Entry(model).State = EntityState.Modified;
}
else
{
   _context.Entry(model).State = EntityState.Add;
}

Conclusion

While we coding seriously in our app then suddenly can trap up in few unexpected issues i.e. Store update, insert, or delete statement affected an unexpected. I have provided complete to recover unexpected issue. Hope it will help to all who eventually got in this issue i.e. Store update, insert, or delete statement affected an unexpected.

Suggested Reading

Share

Anjan kant

Outstanding journey in Microsoft Technologies (ASP.Net, C#, SQL Programming, WPF, Silverlight, WCF etc.), client side technologies AngularJS, KnockoutJS, Javascript, Ajax Calls, Json and Hybrid apps etc. I love to devote free time in writing, blogging, social networking and adventurous life

Post A Comment:

0 comments: