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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:25:15+00:00 2026-05-26T08:25:15+00:00

I have a business case in which, for each record in a given source

  • 0

I have a business case in which, for each record in a given source table, a number of changes in different tables need to be made, and each of these sourceTable records need to be processed in isolation.

So I have the following pseodocode:

MyEntityFrameworkContext ctx;
foreach (sourceRecord sr in ctx.sourceTable)
{
    try
    {
        using (MyEntityFrameworkContext tctx = new MyEntityFramworkContext)
        {
            string result1 = MakeUpdatesToSomeOtherTable1(tctx);
            sr.Result1 = result1;
            string result2 = MakeUpdatesToSomeOtherTable2(tctx);
            sr.Result2 = result2;
            // will be more tables here.
            using (TransactionScope ts = new TransactionScope)
            {
                tctx.SaveChanges; // to save changes made to OtherTable1 and    OtherTable2
                tctx.ExecuteStoreCommand("SQL that makes a few other changes related to    sourceRecord to tables that are NOT in the EF context");
                ts.Complete();
            }
        }
    }
    catch (Exception ex)
    {
     sr.ExceptionResult = ex.Message();
    }
});
ctx.SaveChanges(); // to save all changes made to sourceTable.

The reason for the tctx and TransactionScope within the loop is that I need the changes to the OtherTables1&2 to be saved in one transaction with the call to the tctx.ExecuteStoreCommand() for each sourceRecord being processed.

Note also that I need the results written to sourceTable to be saved independently of the changes made to the tables updated in the TransactionScope. Hence I can’t include the update of sourceTable in the same TransactionScope, since if that txn rolls back, I won’t have a record of the exception. This way, at the end of the whole process, I can see which sourceRecords failed and which ones succeeded.

The above pseudocode works perfectly.

However, I would like to take advantage of parallelism here, and have converted the foreach to a Parallel.ForEach(). But then I run into very unexpected errors (like TransactionAbortedException after calling ts.Complete(), NullReferenceException when calling ctx.SaveChanges(), or when setting one of the Result properties of sourceRecord, I sometimes get InvalidOperationException: EntityMemberChanged or EntityComplexMemberChanged was called without first calling EntityMemberChanging or EntityComplexMemberChanging on the same change tracker with the same property name).

So I’m thinking that parallelism, while being perfect for QUERIES, does not lend itself well to data UPDATES in EntityFramwork? What am I missing, or not understanding about parallelism? I don’t understand why my above approach breaks when converted to use parallelism. Any advice would be appreciated.

  • 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-26T08:25:15+00:00Added an answer on May 26, 2026 at 8:25 am

    The EF object context isn’t thread safe, so there is a high potential for catastrophic errors when multiple threads are buzzing around in the same context.

    It looks like you have at least one context object outside the foreach loop and shared among threads.

    Based on your description I’d guess that updating properties on the sourceRecord entities from multiple threads is corrupting some internal state in the context – probably the collections of data it maintains for change tracking.

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

Sidebar

Related Questions

I have a 'Business' model which has_many 'Hours'. Each Hour record has a start_time
I have a business case whereby I need to be able to specify my
I have a business class which I need to serialize to xml. It has
I'm working on a web-based business application where each customer will need to have
We have a business case that would be perfect for multiple BackgroundWorkers. As an
I have some business objects which use: Web.Configuration.WebConfigurationManager.AppSettings.Item(SomeSetting) Now that I'm breaking those objects
I have a business object project, which contains composite structure: public class Tree {
I have a JUnit4 test which extends AbstractTransactionalJUnit4SpringContextTests. I have a business object that
Let's say we have a business object, let's call it a Foo , which
I have a table that maintains specific business transactions, let's call it LOANS .

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.