I have been dealing with this for 2 days and I can't find solution.
using (TaxablePersonContext context = new TaxablePersonContext(this.ConnectionString))
{
context.Configuration.AutoDetectChangesEnabled = false;
foreach(TaxablePerson p in persons) // Persons has always size 1000
{
// TaxablePerson has some other properties e.g. Name, VatId, Street,...
p.RecCreatedBy = "application name";
p.RecCreatedOn = this.SynchronizationStartDateTime;
p.RecModifiedBy = "application name";
p.RecModifiedOn = this.SynchronizationStartDateTime;
p.RecSyncDate = this.SynchronizationStartDateTime;
p.RecActive = true;
}
DateTime start1 = DateTime.Now;
context.TaxablePersons.AddRange(persons);
TimeSpan end1 = DateTime.Now.Subtract(start1);
DateTime start2 = DateTime.Now;
context.SaveChanges();
TimeSpan end2 = DateTime.Now.Subtract(start1);
}
I takes nearly 10 seconds to insert 1000 records and 98 seconds to insert 10.000 records in sql server. Can you please advise what to do to improve Entity framework insert performance. I read this post Fastest Way of Inserting in Entity Framework and included the tips mentioned in this post, but still insert is very slow. I need to insert 260.000 records which takes 52 minutes. I'm inserting in batches of 1000, which upper code demonstrates. Data is read from file, when I hit 1000 records I do synhronization with database. What else can I do? Some people mention that when used setting context.Configuration.AutoDetectChangesEnabled = false; performance improved from minutes to nearly some seconds. What am I missing? I'm using entity framework 6.1.3.