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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T05:54:40+00:00 2026-06-15T05:54:40+00:00

The following method is causing the FIRST db.SaveChanges() to throw an exception: (New transaction

  • 0

The following method is causing the FIRST db.SaveChanges() to throw an exception:
(New transaction is not allowed because there are other threads running in the session.)

protected void btnInsertDependencies_Click(object sender, EventArgs e)
{
    using (icmsEntities db = new icmsEntities())
    {
        var divisions = db.Divisions;
        foreach (var division in divisions)
        {
            var dlc_sectors = db.Sectors.Where(s => s.Website.division_id == division.division_id).DistinctBy(s => s.name).ToList();
            foreach (var sector in dlc_sectors)
            {
                dlc_Sector dlc_sector = new dlc_Sector
                {
                    name = sector.name,
                    division_id = sector.Website.division_id
                };

                db.dlc_Sectors.AddObject(dlc_sector);

                var dlc_products = db.Products.Where(p => p.Sectors
                    .Any(s => s.Website.division_id == dlc_sector.division_id && s.name == dlc_sector.name))
                    .DistinctBy(p => p.name).ToList();

                foreach (var product in dlc_products.ToList())
                {
                    var dlc_product = db.dlc_Products.SingleOrDefault(p => p.name == product.name);
                    if (dlc_product == null)
                    {
                        dlc_product = new dlc_Product { name = product.name };
                        db.dlc_Products.AddObject(dlc_product);
                    }
                    dlc_product.dlc_Sectors.Add(dlc_sector);
                    db.SaveChanges();
                }
            }
        }
        db.SaveChanges();
    }
}

The first db.SaveChanges() is to make sure that a new product having the same name does not get inserted again.

How can I resolve this? Thanks in advance.

Worked for me:

                    var Local = db.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Added)
                        .Where(es => es.Entity is dlc_Product).Select(es => es.Entity as dlc_Product);
                    var dlc_product = db.dlc_Products.SingleOrDefault(p => p.name == product.name)
                        ?? Local.SingleOrDefault(p => p.name == product.name);

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-15T05:54:42+00:00Added an answer on June 15, 2026 at 5:54 am

    Not sure why this occurs, but the first SaveChanges() would not be necessary if you also checked the Local collection of db.dlc_Products:

    foreach (var product in dlc_products.ToList())
    {
       var dlc_product = db.dlc_Products.SingleOrDefault(p => p.name == product.name)
           ?? db.dlc_Products.Local.SingleOrDefault(p => p.name == product.name);
        ...
    }
    

    New objects are added to the Local collection.

    Note that db.dlc_Products.Join(db.dlc_Products.Local) looks more efficient, but it does not compile, and the other way around it queries the whole db.dlc_Products for each call.

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

Sidebar

Related Questions

The following method calculates cost. It access other classes when required. Cloud class holds
The following method is launched from the constructor of UserControl. A cross thread exception
The following method does not apply the wpf changes (background = red) until the
The following method does not access any shared variable. It is still not thread
Following methods gets called from didFinishLaunchingWithOptions,when I run on Instruments openDatabase method causing leaks..
The following method, when called with something like String val = getCell(SELECT col FROM
The following method does its job but keeps printing a warning also, im pretty
I have the following method, which takes in the name of a file as
I have the following method that returns void and I need to use it
I have the following method where I want to test the event.status property only

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.