When adding and removing records, DbContext is very sluggish.

dbcontext entity-framework

Question

I discovered that adding and removing entities using DbContext in a database-first situation is much slower than with ObjectContext. DbContext is 3 to 5 times slower than ObjectContext when adding 2000 entities and storing the changes at the end (I realize that using SqlBulkCopy would be preferable for adding a big number of entities, but that's not the issue). Even if modifications are saved after each addition, DbContext is still about twice as sluggish. Even worse is what happens when anything is deleted: DbContext is around 18 times slower than ObjectContext when saving after all entity deletions.

I used a tiny console program to double-check and my extensively built test application I use to evaluate database access technologies. For both adding and removing entities using DbContext, poor results were shown. The outcomes of the console application are as follows:

Inserting 2000 entities via DbContext saving changes at the end: 2164ms
Inserting 2000 entities via ObjectContext saving changes at the end: 457ms
Inserting 2000 entities via DbContext saving changes after each object addition: 8420ms
Inserting 2000 entities via ObjectContext saving changes after each object adding: 4857ms
Inserting 2000 entities via DbContext using a new DbContext for each object addition: 4018ms
Deleting 2000 entities via DbContext saving changes at the end: 4794ms
Deleting 2000 entities via ObjectContext saving changes at the end: 261ms
Deleting 2000 entities via DbContext saving changes after each object deletion: 25536ms
Deleting 2000 entities via ObjectContext saving changes after each object deletion: 2110ms

With roughly identical results, I tried using EF 4.3 with VC 2010 and EF 5.0 Beta 2 in VS 11. The "EF 4.x POCO Entity Generator for C#," the "EF 4.x DbContext Generator for C#," and the "EF 5.x DbContext Generator for C#" all offered T4 templates, which I utilized.

What may be a problem? I would never utilize DbContext in an application that has to add or remove entities, according to the test results (what makes DbContext unfortunately unusable for me).

I installed the console test programs DbContext Test for EF 4.3 and DbContext Test for EF 5.0 on my web server.

We welcome any suggestions or amendments.

1
46
4/11/2012 9:53:48 AM

Accepted Answer

Try include the following in your DbContext tests:

dbContext.Configuration.AutoDetectChangesEnabled = false;

// Now do all your changes

dbContext.ChangeTracker.DetectChanges();
dbContext.SaveChanges();

Then try running your tests once again.

DbContext API had certain architectural modifications that need it to constantly check for changes to entities.Add , Attach or Delete anything based on the environment. This detection in the ObjectContext API only runs when you trigger it.SaveChanges . Although it needs particular handling for big data processing, it is a superior choice for the majority of frequent circumstances.

92
4/11/2012 9:58:49 AM

Popular Answer

Zzz-5-Zzz and Zzz-9-Zzz may now be used on Zzz-13-Zzz in EF6.

referring to the links' documentation

Note that if AutoDetectChangesEnabled is set to true (which is the default), then DetectChanges will be called once before {adding,deleting} any entities and will not be called again. This means that in some situations {Add,Remove}Range may perform significantly better than calling {Add,Remove} multiple times would do.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow