I have a windows service that runs every 10 seconds ... each time it runs, it takes some test data, modifies it and persists it to the database using the EntityFramework. However, on every second run, when I try to persist the change I get the following Optimistic Concurrency Exception:-
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 know for a fact that there is nothing else writing to that DB but my service which updates records every 10 seconds. What could be causing the concurrency exception here ?
I think a related entity somewhere in the object graph was getting modified prior to the second save operation. All i am doing really is instantiating a new object context, and calling a save operation on some records i had retrieved using the same context. The following code worked ---
var ctx = new blahEntities();
var profile = ctx.ProfileSet.Where(pr=>pr.FirstName.Contains("a")).FirstOrDefault();
profile.Address = "modified";
ctx.SaveChanges();
ctx.Refresh(RefreshMode.StoreWins,profile);