1

I am using EF 4.2 and am having an issue that happens quite randomly and without warning. I have a Windows Service which updates the database. In the service I have a timer. When the time of the timer elapses a method gets called. This is the basic structure of the method

IEnumerable<Foo> foos = GetFoosFromDB(); foreach (Foo foo in foos) { if (some condition) { foo.Bar = 1; } if (some other condition) { foo.Bar = 2; } if (yet some other condition) { foo.Bar = 3; } else { int val = GetSomeValueFromDB(); if (val == something) { if(GetSomeOtherValueFromDB()) { foo.Bar = 4; } else { CallSomeMethodThatAlsoCallsSaveChanges(); foo.Bat = SomeCalculatedValue(); } } } } SaveChanges(); 

Now, the problem is that once we start working with the database for a day and there are a few rows in the tables of that database (we are talking about 100 or 200 rows only), then even though this method is called, SaveChanges doesn't seem to do what it should do. What am I doing wrong?

thanks,

Sachin

3
  • what do you have on the log files ? Commented Jul 18, 2012 at 17:16
  • Do CallSomeMethodThatAlsoCallsSaveChanges and SaveChanges() use different context instances? Commented Jul 18, 2012 at 17:56
  • No logs I'm afraid. No, they use the same context. Commented Jul 19, 2012 at 9:28

1 Answer 1

1

Ignoring the other aspects of the code, this line stuck out as a likely problem:

else { CallSomeMethodThatAlsoCallsSaveChanges(); foo.Bat = SomeCalculatedValue(); } // a few }} later... SaveChanges(); 

When this logic branch is executed, your context's pending changes are committed to the DB (based on what you've provided). Depending on how you're creating and managing your db context objects, you've either cleared the modified list, or you've introduced potential change conflicts. When SaveChanges() is called after the loop, it may or may not have pending changes to commit (depends on whether the conditional logic called your other method).

Consider what logical unit(s) of work are being performed with this logic and keep those UoW atomically separated. Think about how your DB contexts are being created, managed, and passed around, since those maintain the local state of your objects.

If you are still having trouble, you can post more of your code and we can attempt to troubleshoot futher

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.