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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:51:41+00:00 2026-06-07T16:51:41+00:00

In my EF4 program, I have an Applicant and Application table. Multiple instances of

  • 0

In my EF4 program, I have an Applicant and Application table. Multiple instances of the program runs periodically to create application for applicants based on some business logic. In Application table I cannot have more than one Submitted/BeingSubmitted records for an Applicant.

So here’s the piece of code which checks if there’s an Submitted/BeingSubmitted application and inserts it. It is run inside a foreach loop for a list of Applicants.

public Application SaveApplication(Int32 applicantId)
    {
        using (TransactionScope txScope = new TransactionScope(TransactionScopeOption.RequiresNew))
        {
            if (ApplicantHasPendingApplication(applicantId))
                return null;

            Application app = null;
            try
            {
                app = new Application()
                {
                    // Create the object...
                };

                _unitOfWork.DisclosureApplications.Add(app);
                _unitOfWork.Commit();
                _unitOfWork.Refresh(app); // We save and refresh it to get the Id.

                txScope.Complete();
            }
            catch (UpdateException ex)
            {
                // We get an Update exception here when multiple instances tries to insert Application.
            }

            return app;
        }
    }

Above piece of code prevents insertion of duplicate records apart from the fact that it throws UpdateException while running multiple instances of the program. If I swallow that exception and carry on then everything is fine.

However, I tried to test/run above code in parallel but it inserts duplicate records in database.

Parallel.Invoke(
            () => CreateApplications("Parallel Instance 1"),
            () => CreateApplications("Parallel Instance 2"));

private void CreateApplications(String dummyInstanceName)
{
   var unitOfWork = new SqlUnitOfWork();
   var applicants = unitOfWork.Applicants.FindAll().Take(100).ToList();

   var facade = new ProviderFacade(unitOfWork, new Log4NetLogger(dummyInstanceName));

   foreach (Applicant applicant in applicants)
            {
                facade.ApplicationProvider.SaveApplication(applicant.applicantID);
            }
}

In above piece of code, it throws UpdateException and inserts multiple Application row for an Applicant.

Note that, the table only has a surrogate primary key and no other unique constraints.

My question is: Why the TransactionScope inserts duplicate rows by running it in Parallel.Invoke but not when I fire off multiple instances of the program? What would be a sound approach achieving it?

Update: The ctor of SqlUnitOfWork is

public SqlUnitOfWork()
    {
        _context = new MyEntities();
    }

The ctor of MyEntities is generated by EF –

    public const string ConnectionString = "name=Entities";
    public const string ContainerName = "Entities";

    public TPIEntities() : base(ConnectionString, ContainerName)
    {
        this.ContextOptions.LazyLoadingEnabled = true;
    }

Thanks.

  • 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-07T16:51:43+00:00Added an answer on June 7, 2026 at 4:51 pm

    This is a classic race condition. Both transactions check to see if a pre-existing Application exists. Both find that there is none. Then, both try to insert.

    You need to synchronize your checking, either by locking in the application or by taking a SQL Server UPDLOCK, HOLDLOCK when checking whether the Application exists.

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

Sidebar

Related Questions

I am using EF4/RIA combo in a Silverlight application. I have multiple service methods
I have an EF4.1 database first application, and the .ToString() method is only giving
I'm working with EF4.1 Code First and am trying to create some Many-to-Many relationship
I have a codefirst EF-4.1 based program. The user gets a context and can
In EF4 is it possible to have a referential contstraint which is based upon
I'm making a database in EF4.1 Code First. I have a table, MedicalPlan, with
Using EF4 and Linq I have a data structure which looks like this: parentid
I'm learning EF4.1 against the northwind database. I have created a POCO class like
I am using EF4 with the EDM designer. I have two tables : Users
I have an MVC3/EF4.1 app that I'm building which now works fine on my

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.