Geeks With Blogs
Fórmulas e Cenas Object Reference Not Set to an Instance of an Object

When you are inserting into a table using entity framework, on rare occasions you might get the following error:

 {System.Data.OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0)


This happened to me because of an "instead of  INSERT" trigger that was active on the page. It turns out that EF does not go well with triggers, because it fails to get the inserted ID.

For me, the solution was to change the trigger so that the last executed code was a select, something like this:

select top 1 id from MyTable order by id desc


Hope this helps you :)


Posted on Tuesday, January 22, 2013 10:35 AM Geral , Asp.Net , C# , Database | Back to top

Comments on this post: Entity Framework, Insert Fail

# re: Entity Framework, Insert Fail
Requesting Gravatar...
Great idea! This is a timely information for the project that I am doing now. - Paradise Home Improvement Charlotte
Left by Elvy Ashton on Nov 12, 2016 1:45 AM

Your comment:
 (will show your gravatar)

Copyright © Sglima | Powered by: