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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:41:47+00:00 2026-05-30T13:41:47+00:00

I am using AutoMapper to map objects from a legacy database to a new

  • 0

I am using AutoMapper to map objects from a legacy database to a new database declared in EF code first:

  StartTimer("Map Customers");      
      var mfpCustomers = Mapper.Map<IEnumerable<LegacyDataModel.Customer>, IEnumerable<Customer>>(
        legacyEntities.Customers.Include(p => p.Demographics).ToList());
      StopAndPrintTimer("Map Customers");

      StartTimer("Iterate Customers");
      foreach (var p in mfpCustomers)
      {
        db.Customers.Add(p);       
      }
      StopAndPrintTimer("Iterate Customers");

The for each is what takes the most time. I would like to map directly to a DbSet(db.Customer), instead of doing a foreach over db.Customers.Add(p);. I haven’t figured out how to do this though as there is nothing like an AddRange method designed specifically for adding large numbers of objects. I know how to do these types of transformations with set based techniques in SQL, but that is an insane amount of typing because the intellisense doesn’t work that great in those scenarios in SSMS.

This code doesn’t need to be super fast, but having to wait a full minute every time I seed the database is making development hard(and it does need to be reseeded often due to rapid development of the new database). Note also that since this is the seed method, I don’t mind completely replacing the existing Customers(since there are none). So it doesn’t need to behave like AddRange in that respect, as I’m really just creating a new collection.

Any ideas on how I can improve the performance here?
Any ideas on how I can map the entire collection directly into the DbSet without doing a foreach on every instance?

  • 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-30T13:41:48+00:00Added an answer on May 30, 2026 at 1:41 pm

    Each time you call db.Customers.Add(p) EF is making a DetectChanges call to see if anything in your entity graph has changed. This helps make things work as expected when Add is called after making changes to other entities. However, DetectChanges can get slow if there are a lot of entities in the context–it has O(n) time on the number of entities. So in cases like you have it can make a big difference if you temporarily turn of these automatic calls to DetectChanges. For example:

    try
    {
        context.Configuration.AutoDetectChangesEnabled = false;
    
        foreach (var p in mfpCustomers)  
        {  
            db.Customers.Add(p);         
        }  
    }
    finally
    {
        context.Configuration.AutoDetectChangesEnabled = true;
    }
    

    You can find more details here:
    http://blogs.msdn.com/b/adonet/archive/2011/02/06/using-dbcontext-in-ef-feature-ctp5-part-12-automatically-detecting-changes.aspx

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

Sidebar

Related Questions

I am using AutoMapper in my ASP.NET MVC website to map my database objects
I am using AutoMapper to datareader using code as discussed below http://elegantcode.com/2009/10/16/mapping-from-idatareaderidatarecord-with-automapper/ I see
I am using automapper (successfully up to a point) to perform a polymorphic map
I'm using AutoMapper to map between a Linq Domain object and a ViewModel to
I have this interface for using AutoMapper: public interface IMapper { object Map(object source,
I'm using AutoMapper to mapping my Entity Framework POCO objects and ViewModels. Question 1:
In my ASP.NET MVC 2 (RC) project - I'm using AutoMapper to map between
Folks, I am mapping EF objects to simple Pocos using automapper in my service
We are using AutoMapper from Codeplex and for me the destination object has all
I cannot seem to map a custom type using the latest autoMapper library in

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.