Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I speed up DbSet.Add()?

I have to import about 30k rows from a CSV file to my SQL database, this sadly takes 20 minutes.

Troubleshooting with a profiler shows me that DbSet.Add is taking the most time, but why?

I have these Entity Framework Code-First classes:

public class Article {     // About 20 properties, each property doesn't store excessive amounts of data }  public class Database : DbContext {     public DbSet<Article> Articles { get; set; } } 

For each item in my for loop I do:

db.Articles.Add(article); 

Outside the for loop I do:

db.SaveChanges(); 

It's connected with my local SQLExpress server, but I guess there isn't anything written till SaveChanges is being called so I guess the server won't be the problem....

like image 307
Tamara Wijsman Avatar asked Dec 04 '10 19:12

Tamara Wijsman


People also ask

What is the difference between DbSet and DbContext?

Intuitively, a DbContext corresponds to your database (or a collection of tables and views in your database) whereas a DbSet corresponds to a table or view in your database. So it makes perfect sense that you will get a combination of both!

What does DbSet mean?

A DbSet represents the collection of all entities in the context, or that can be queried from the database, of a given type. DbSet objects are created from a DbContext using the DbContext.

What does DbSet attach do?

Attach is used to repopulate a context with an entity that is known to already exist in the database. SaveChanges will therefore not attempt to insert an attached entity into the database because it is assumed to already be there.

How do I refresh EF context?

The best way to refresh entities in your context is to dispose your context and create a new one.


1 Answers

As per Kevin Ramen's comment (Mar 29) I can confirm that setting db.Configuration.AutoDetectChangesEnabled = false makes a huge difference in speed

Running Add() on 2324 items by default ran 3min 15sec on my machine, disabling the auto-detection resulted in the operation completing in 0.5sec.

http://blog.larud.net/archive/2011/07/12/bulk-load-items-to-a-ef-4-1-code-first-aspx

like image 141
Rudi Avatar answered Sep 20 '22 23:09

Rudi