Monday, August 29, 2016

Concurrency violation: the UpdateCommand affected 0 of the expected 1 records.

This one annoyed me enough to dust off the blog. I created a simple data-bound form in C# with Visual Studio 2015. It's just a grid bound to a data table and when I click a button, the data is saved. It's the "Hello World" of database applications just so I can key in some data easily.

Everything's working great. The app functions as it's supposed to doesn't. All of a sudden, I start to see this error "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records." I google it and see folks talking about actual concurrency issues (not happening here, I'm the only one using the database) and even date/time settings. Nothing that would cause me to have this error message

Finally, I just fire up the SQL Profiler and trace what's happening behind the scenes. I can see the update statement that's sent by Entity Framework is doing an update with a condition where field_value = @original_field_value. That's it. The original field value was null and any test against null fails. It turns out that the original dataset was defined when the field wasn't nullable. When I updated the table to allow nulls, I never updated the dataset definition. Once I did that in the dataset editor, the update statement was modified automatically to handle null values correctly.

No comments:

Post a Comment