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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:41:59+00:00 2026-05-31T04:41:59+00:00

We have a huge performance problem in our app, we collect data from different

  • 0

We have a huge performance problem in our app, we collect data from different systems and save it in our DB to later generate some reports.

We use Entity framework 4.2 and DbSets, is there a better way of doing bulk inserts then the Add method for each entity on the DbSet? Because there are huge performance problems with the Add logic

oneThousandCustomers.Foreach(c => context.Customers.Add(c)); //This will take a minute
context.SaveChanges(); //This takes under a second

I guess the add method is doing lots lookups etc and stuff under the hood, but I just want to bulk insert this data. Is it possible?

  • 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-31T04:42:00+00:00Added an answer on May 31, 2026 at 4:42 am

    Ok so the deal here is that EF does a whole lot of stuff under the scenes when you add an item to a collection.

    The really big thing in terms of performance is that it calls DetectChanges() automatically on each add command which enumerates the entire object graph.

    What this means is that if you have a large amount of items which are being tracked by the current EF context this will cause some really serious performance issues.

    Heres some tips to improve performance, as a comparison i can insert 1000 fairly simple entities with no FK’s to EF in under 1/2 a second.

    • Make sure you are using a new EF context instance for your query,
      this will ensure that your change graph is small so add will be
      fairly quick.
    • Turn of auto change detection for the DbContext

    Code to turn off auto-detect changes

    context.Configuration.AutoDetectChangesEnabled = false;
    

    When disabling AutoDetectChanges you need to be a little careful as it means that most of the EF automagic is turned off so you may get some weirdness especially if you are using navigation properties or are updating existing entities. To solve these issues call DetectChanges just before you call SaveChanges. This will decrease performance and isn’t needed with simple add operations so in your example you can probably get away without this.

    This MSDN article talks about this, (note even though its EF 5 its the same in 4) http://msdn.microsoft.com/en-us/library/gg696177%28v=vs.103%29.aspx

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

Sidebar

Related Questions

I have a huge web app that is having issues with memory leak in
I have a huge database with some 100 tables and some 250 stored procedures.
I have a huge array that has to be read by different threads in
Given I have a HUGE array, and a value from it. I want to
We have a web app + web site, which has performance issues. I have
Problem: i am trying to fetch huge number of entries from the database and
I have to use a derby with some huge tables in a project. To
I have noticed a huge performance hit in one of my projects when logging
I have huge 3D arrays of numbers in my .NET application. I need to
I have huge number of Word files I need to merge (join) into one

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.