1

I have a simple console app written in C# with VS.NET 2012. I'm using a simple database with Entity Framework 5 (model first). I have two tables, Customer, and Email. Every email record has a reference to a customer record.

I create the customer and email object, save them to the collection in the EF context, and after 1000 records have been created, I call save changes on the context. The batches of 1000 become increasingly slow, starting out with 1 minute for 1000, and then by the 9000th record, the batches of 1000 take 4 minutes to complete.

I am reading the data out of a CSV file one line at a time, doing a simple split on each line, and then constructing the simple objects.

My question is whether this is EF5, or if I'm missing an index on the database? I'm doing no other pre-processing. I'm using SQL Server 1012.

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
yarning
  • 1,463
  • 2
  • 9
  • 6

2 Answers2

2

Your DbContext tracks all objects, even after they are saved. The size of the DbContext is probably what is causing your application to become slow.

I recommend disposing the DbContext and creating a new DbContext after 1000 inserts.

SchmitzIT
  • 9,227
  • 9
  • 65
  • 92
Johan Gorter
  • 1,253
  • 10
  • 13
  • This improved things, although it's still 3x slower than using the SQL helper classes and stored procs. But there's another problem, it seems by not calling save on every new record, that queries to that context fail to see the newly added and yet to be saved records, could that be true? – yarning Nov 17 '12 at 23:11
0

you can disable the track of object using this

context.Configuration.AutoDetectChangesEnabled = false;
Eranga
  • 32,181
  • 5
  • 97
  • 96
shiva
  • 36
  • 1