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 7308935
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:38:30+00:00 2026-05-28T23:38:30+00:00

I have an EF code-first generated database. Data manipulation is done using DbContext .

  • 0

I have an EF code-first generated database. Data manipulation is done using DbContext. An IoC container in Asp.net application generates a DbContext instance, that BL objects rely on on a per thread basis. The is a background task class that is loaded along with a web application.

Every one in a while (like every 10 minutes), a background thread adds one item to a list of Incidents by calling myDbContext.Add within a transaction scope.

Meanwhile, it seems like if one of the Incidents is changed by myDbContext in a “web requests thread”, even though the changes are saved to a database for a moment, they are getting overridden by the set of the Incidents, that were pulled by the backgound thread a few moments before the user used the web page to alter an Incident.

This seems like a concurrency problem (and I don’t implement any concurency like Timestamp columns).

My question is: Shouldn’t the background thread only save changed data (in my case, adding a new incident), leaving an entire incidents collection? If it is indeed true, my problem source is somewhere else.

Code from a background thread:

using (var transaction = new TransactionScope())
            {
                foreach (var scheduledTask in _db.ScheduledTasks)
                {
                    if (scheduledTask.NextExecuteAfterDate == null)
                    {
                        PopulateNextExecuteAfterDate(scheduledTask);
                        shouldSaveChanges = true;
                    }

                    if (DateTime.Now > scheduledTask.NextExecuteAfterDate)
                    {
                        RegisterRecurringTicket(scheduledTask);
                        CalculateNextTime(scheduledTask);
                        shouldSaveChanges = true;
                    }
                }

                if (shouldSaveChanges) _db.SaveChanges();
                transaction.Complete();
            }

The code in subroutine RegisterRecurringTicket(scheduledTask); adds item to an Incidents collection. When a _db.SaveChanges(); is called, it seems like the collection of incidents is overwritten with the older set of incidents, casting the UI-generated changes to an Incidents collection obsolete. If so, how can I solve this problem?

  • 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-05-28T23:38:31+00:00Added an answer on May 28, 2026 at 11:38 pm

    The problem was that I was using a PerThreadScope DbContext injection. This has resulted in an unexpected behaviour of my ASP.NET application. Setting the Ninject Module to resolve DbContext in PerRequestScope fixed the problem.

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

Sidebar

Related Questions

Background We have a WinForms application with Entity Framework 4.2 code-first / FluentAPI using
I have an AXIS2/JAX-WS web service using a code first implementation (yes I know,
I have been using this code with great success to pull out the first
I have an ASP.NET MVC 3 app that is using SQL Server CE 4.0
I've been making my first ASP.NET Visual Studio website and I have just started
I have a project in which I am using .nettiers generated code as my
Racking my brains on this one. I have the code below: the first stages
I have this code: tableList = [[NSMutableArray alloc] initWithObjects:@First View,@Second View,nil]; I have synthesized
Hi I have the code below, once the first (only) item in the menu
I have the following code: Public Shared Function GetAvailableManufacturers() As List(Of Manufacturer) 'first get

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.