Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8238605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T19:52:18+00:00 2026-06-07T19:52:18+00:00

I am using EF 4.2 and am having an issue that happens quite randomly

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T19:52:19+00:00Added an answer on June 7, 2026 at 7:52 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having an odd issue in FF10 using jQuery 1.7.1. I have a gallery
I am having a very strange issue in an activity using a listview that
Im having an issue using Java script to create a table that displays an
I'm having an issue that's incredibly hard to debug. If my app is using
I'm having a number of related issues using AVExportSession. I'm very aware that I
I am having a issue using Google's SDC (Secure Data Connector) with my Google
I'm having an issue using the Cookie class of the Servlet API 2.5 on
Using RAILS 2.2 Hello I am having an issue where my dates are being
I'm having an issue using the qTip plugin with jQuery. I've created a js
I'm having an issue verifying if a checkbox is checked using jquery on Internet

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.