문제

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

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top