4

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);
4
  • I assume you are calling SubmitChanges(). You might try creating a fresh copy of your ObjectContext each time. Commented May 24, 2010 at 23:30
  • Yes I call "SaveChanges" which is the EF equivalent of "SubmitChanges". When you say "Create a fresh copy of your ObjectContext do you mean each time the service enters the loop, you want a new objectcontext to be passed ? Right now, the object context is created once when the service starts and it is reused to perform DB operations. Is there a way to refresh the existing context ? Commented May 24, 2010 at 23:45
  • Can you post some code to illustrate how you are modifying your entities, because i have seen this happen as a result of attaching/detached entities. Commented May 25, 2010 at 1:34
  • 1
    So I resolved the issue by forcing a Refresh(RefreshMode.StoreWins, entity) after a successful save. I read that to be a recommended step here - msdn.microsoft.com/en-us/library/bb738618.aspx Commented May 25, 2010 at 2:11

2 Answers 2

6

The "unexpected number of rows (0)" indicates that more than likely you don't have an ID field correctly mapped or defined in your entity model, or you are modifying a detached entity and attempting to save it and it doesn't have key information.

Run SQL Server profiler, or Entity Framework Profiler, and see what is going on in the background.

Sign up to request clarification or add additional context in comments.

1 Comment

Just had the same problem
2

Once had this same issue and spent hours tracking it down to a malfunctioning update trigger. If you have any triggers on the table that's being updated, make sure they're working correctly. I work in an Oracle environment, fwiw.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.