0

I am loading a list of objects, need to map those objects and store it back to another database. Those two tables don't have the same schema, so a mapping is necessary.
For example:

using (var db1 = new Db1())
{
    var customers = db1.CustomersDb1;

    using (var db2 = new Db2())
    {
        foreach (var customer in customers)
        {
            var c = Mapper.Map<CustomerDb2>(customer);
            db2.customers.Add(c);
        }
        db2.SaveChanges();
    }
}

Now i tested this piece of code with round about 12000 entries and it lasted 2-3 minutes just until i reach db2.SaveChanges(). So i even haven't stored anything after this time.
The Customer object in Db2 just has 14 properties so i don't exactly know where it needs this much time.

Is there a faster way to map and copy two tables with Entity Framework?

EDIT:
The most time is needed in the loop. So it's not a problem of the database. And it also doesn't matter if i use automapper or if i map it manually.

Obl Tobl
  • 5,604
  • 8
  • 41
  • 65
  • possible duplicate of [Which is faster: Automapper, Valuinjector, or manual mapping? To what degree is each one faster?](http://stackoverflow.com/questions/8122334/which-is-faster-automapper-valuinjector-or-manual-mapping-to-what-degree-is) – CodeCaster Nov 13 '13 at 11:22
  • Map them manually or with another mapper, compare the result and pick the fastest. – CodeCaster Nov 13 '13 at 11:23
  • @CodeCaster it doesn't matter how i map it, i edited my question. – Obl Tobl Nov 13 '13 at 11:25
  • 1
    You might benefit from researching alternative methods for bulk inserts (I don't know how to do that with EF off the top of my head). For example, `SqlBulkCopy` or perhaps NHibernates stateless sessions combined with a GuidComb. I am going to assume that most of the CPU time is spent tracking/housekeeping the 12000 objects passing through EF. – Simon Whitehead Nov 13 '13 at 11:50
  • @SimonWhitehead thanks for your comment. i debugged and took time. Most of the time really is lost with adding the object to Entity Framework. Maybe there is no faster solution. – Obl Tobl Nov 13 '13 at 12:05
  • have you checked these? http://stackoverflow.com/questions/18497238/improve-performance-of-inserting-data-for-one-to-many-relationship-in-ef and http://stackoverflow.com/questions/17274430/efficient-way-to-do-bulk-insert-update-with-entity-framework – phil soady Nov 13 '13 at 12:07
  • Another way besides SqlBulkCopy are ETL products like SSIS and RhinoETL – LostInComputer Nov 13 '13 at 12:12

1 Answers1

0

Don't use EF. Use just a single SQL statement.

INSERT INTO Db1.Customers (ID, Value1, Value2)
SELECT NEWID(), Value1, Value2 FROM Db2.Customers

GO 
Jimmy Bogard
  • 26,045
  • 5
  • 74
  • 69