Stopbyte

EF Exception: “Store update, insert, or delete statement affected an unexpected number of rows (0).”

I am getting this error when trying to save DataContext Data into Database:

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.

I don’t know what’s wrong as everything seems normal, and all other Storage Calls gets fulfilled except this one below give that error:

App.DataContext.SContext.Provisions.Add(new Model.Provision()
{
    LeadProvision = App.Locator.ContractModelView.SelectedProvision.ID,
    Logic = btn_one_none.IsChecked == true ? "AND" : "NOT",
    AnotherProvision = cb_another_provision.SelectedValue == null ? false : true,
    Type = item,
    AuditKey = App.AAK,
});
 
/* The error is raised here. */
App.DataContext.SContext.SaveChanges();
3 Likes

Maybe there are some concurrent Changes occurring on the Database at the same time, that’s the main reason that error is thrown i guess.

3 Likes

I had similar issue before, mainly the MSDN articles say that this is caused by concurrent changes occurring to the database at the same time.

But in my case it was caused by a weird Database structure mistake, my Database table that was affected didn’t have any Primary Key in the SQL Server Database structure, while in the EntityFramework table another Field was marked as Primary key, as EntityFramework requires all entities to have at least one Primary key.

So the bottom line is that i believe the issue you are having is only caused by some Database structure design mistake, in my case i solved that by adding a primary key to the database table, and making it an IDENTITY column.

hope those tips help you solve the problem, otherwise you will have to post more code related to your issue, to help the community better understand your “bug”.

good luck.

2 Likes

Solution -
err1
Solution Description -

This was happening because the entity’s ID (key) field not being set. So when the data was getting saved, It was saved with PK field value = 0.

If you updating model from .chtml (view), Make sure add hidden property having PK id.