Note that there are some explanatory texts on larger screens.

plurals
  1. POImproving performance of initializing DbSet in Seed?
    text
    copied!<p>I am using AutoMapper to map objects from a legacy database to a new database declared in EF code first:</p> <pre><code> StartTimer("Map Customers"); var mfpCustomers = Mapper.Map&lt;IEnumerable&lt;LegacyDataModel.Customer&gt;, IEnumerable&lt;Customer&gt;&gt;( legacyEntities.Customers.Include(p =&gt; p.Demographics).ToList()); StopAndPrintTimer("Map Customers"); StartTimer("Iterate Customers"); foreach (var p in mfpCustomers) { db.Customers.Add(p); } StopAndPrintTimer("Iterate Customers"); </code></pre> <p>The for each is what takes the most time. I would like to map directly to a <code>DbSet(db.Customer)</code>, instead of doing a foreach over <code>db.Customers.Add(p);</code>. I haven't figured out how to do this though as there is nothing like an <code>AddRange</code> 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.</p> <p>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.</p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload