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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:04:00+00:00 2026-06-06T13:04:00+00:00

As I understand Working with a DataContext in Parallel is not thread-safe but only

  • 0

As I understand Working with a DataContext in Parallel is not thread-safe but only if you declare your context as static (Based on MSDN documentation)
This is what I did, once I found it worked and the other time it failed !

MSDN :
Thread Safety
“Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.”
http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.aspx

It seems that I’m getting the usual error related to DataContext and it’s Thread-Safety lack.

Here I explain a bit what I want to do, and what is my desired result, hope got expert’s help and ideas on this case.

I want to do heavy analysis processes on about 100,000 records,

The error is :

EntityMemberChanged or EntityComplexMemberChanged was called without first calling EntityMemberChanging or EntityComplexMemberChanging on the same change tracker with the same property name. For information about properly reporting changes, see the Entity Framework documentation.

Pointing to this line in the Designer code :

            ReportPropertyChanged("ProductName");

What is in my Code :

    //On form Load
    if (Repository.ContextP == null)
          Repository.ContextP = new dbEntities();

    private void btnProcess_Click(object sender, EventArgs e)
    {
       Task.Factory.StartNew(() =>
         Process(), TaskCreationOptions.LongRunning); 
    }   

    private void Process()
    {
        var query = from W in Repository.ContextP.Products
                select W;

        Parallel.ForEach(query, options , product =>
        {
           // There are some heavy processes to get stFormattedDef
           product.ProductName= stFormattedDef;               
        }
     }

     // The Repository - static
     static class Repository
     {
         public static dbEntities ContextP { get; set; } 
     }

Security and Data Context Life-time shouldn’t be a problem cause the the analysis is in the build-phase and it’s local.

What I did wrong?

Any suggestions?

Ideas or experiences in similar cases?

  • 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-06T13:04:02+00:00Added an answer on June 6, 2026 at 1:04 pm

    I don’t know EF, but from the question and comments, I think the problem is that you can’t set ProductName from different threads at the same time, even if it is for different Products (because they all use the same non-thread-safe DataContext).

    So, my advice is to run the expensive operation in parallel, but then make sure to set ProductName only from one thread at a time. The simplest way to achieve that is to use a lock:

    var productLock = new object();
    
    Parallel.ForEach(query, options , product =>
    {
       // There are some heavy processes to get stFormattedDef
    
       lock (productLock)
       {
           product.ProductName = stFormattedDef;
       }
    }
    

    Like I said, I don’t know EF, so I don’t fully understand the problem. Because of that, this might not be the right solution.

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

Sidebar

Related Questions

I don't understand why this is not working. It should be very easy. upload.php
I don't understand why atoi() is working for every entry but the first one.
I dont really understand why this is not working, I am being returned to
I don't understand why the first program in JSP is working, but the second
I already had this stuff working in another project but now it does not.
can't understand why this image resizing not working pls help //updating article function updateArticle(){
I don't understand why setTimeout is not working as I expected. I want to
I can't understand why this code is not working. I get an error saying
I don't understand why is this rule not working? If I change total quantity
I dont understand why the following code is not working. I save some inputs

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.