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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:58:08+00:00 2026-06-13T23:58:08+00:00

I wrote a simple import/export application that transforms data from source->destination using EntityFramework and

  • 0

I wrote a simple import/export application that transforms data from source->destination using EntityFramework and AutoMapper. It basically:

  1. selects batchSize of records from the source table
  2. ‘maps‘ data from source->destination entity
  3. add new destination entities to destination table and saves context

I move around 500k records in under 5 minutes. After I refactored the code using generics the performance drops drastically to 250 records in 5 minutes.

Are my delegates that return DbSet<T> properties on the DbContext causing these problems? Or is something else going on?

Fast non-generic code:

public class Importer
{        
    public void ImportAddress()
    {
        const int batchSize = 50;
        int done = 0;
        var src = new SourceDbContext();

        var count = src.Addresses.Count();

        while (done < count)
        {
            using (var dest = new DestinationDbContext())
            {
                var list = src.Addresses.OrderBy(x => x.AddressId).Skip(done).Take(batchSize).ToList();
                list.ForEach(x => dest.Address.Add(Mapper.Map<Addresses, Address>(x)));

                done += batchSize;

                dest.SaveChanges();
            }
        }

        src.Dispose();
    }
}

(Very) slow generic code:

public class Importer<TSourceContext, TDestinationContext>
    where TSourceContext : DbContext
    where TDestinationContext : DbContext
{
    public void Import<TSourceEntity, TSourceOrder, TDestinationEntity>(Func<TSourceContext, DbSet<TSourceEntity>> getSourceSet, Func<TDestinationContext, DbSet<TDestinationEntity>> getDestinationSet, Func<TSourceEntity, TSourceOrder> getOrderBy) 
        where TSourceEntity : class
        where TDestinationEntity : class
    {
        const int batchSize = 50;
        int done = 0;
        var ctx = Activator.CreateInstance<TSourceContext>();
        //Does this getSourceSet delegate cause problems perhaps?

        //Added this
        var set = getSourceSet(ctx);

        var count = set.Count(); 

        while (done < count)
        {
            using (var dctx = Activator.CreateInstance<TDestinationContext>())
            {
                var list = set.OrderBy(getOrderBy).Skip(done).Take(batchSize).ToList(); 
                //Or is the db-side paging mechanism broken by the getSourceSet delegate?
                //Added this
                var destSet = getDestinationSet(dctx);
                list.ForEach(x => destSet.Add(Mapper.Map<TSourceEntity, TDestinationEntity>(x)));

                done += batchSize;
                dctx.SaveChanges();
            }
        }

        ctx.Dispose();
    }
}
  • 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-13T23:58:09+00:00Added an answer on June 13, 2026 at 11:58 pm

    Problem is invocation of the Func delegates you’re doing a lot. Cache the resulting values in variables and it’ll be fine.

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

Sidebar

Related Questions

i have some simple Export/Import Scenario that i can't figur out why this not
I am currently exporting data from an excel import using excel interop. I've been
I wrote simple class that on the start it just increase the value of
I wrote a simple function, that makes emacs add matching quotes (so when I
I wrote a simple plugin which sets some css code using wp_options. It all
For get device id in android sdk i wrote simple This code :: import
I wrote simple benchmark that test performance of multyplying doubles vs BigDecimal. Is my
I wrote a simple script using python-daemon which prints to sys.stdout : #!/usr/bin/env python
We have a drupal site and we wish to export data from this site
I wrote simple monte-carlo π calculation program in Python, using multiprocessing module. It works

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.