Donate

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

As I was creating a simple Create, Update, Delete (CRUD) application in ASP.NET MVC 5 and executing the controller on Edit, I encountered an error called "Store update, insert, or delete statement affected an unexpected number of rows (0)".
After debugging and looking at the stack trace, I found out that the value of the primary key is zero.
Department: "Education"  
Designation: "Dean"
EmployeeName: "JE"
EmployeeID: 0  
Salary: 25500 
The fix for this issue is to add a hidden field to the edit view referencing to the primary key which in this case is the EmployeeID.
@Html.HiddenFor(model => model.EmployeeID)  
And now, the model passed to the context now has a value for the EmployeeID field.
Department: "Education" 
Designation: "Dean" 
EmployeeName: "JE" 
EmployeeID: 2 
Salary: 25500 
Cheers! :)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid