DbContext在添加和删除时非常慢 [英] DbContext is very slow when adding and deleting

查看:148
本文介绍了DbContext在添加和删除时非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当在数据库中使用DbContext时,我发现添加和删除实体与ObjectContext相比非常慢。如果添加2000个实体并保存更改,DbContext比ObjectContext慢3到5倍(btw:我知道使用SqlBulkCopy添加大量实体会更好,但这不是重要的)。如果在每次添加后保存更改DbContext仍然几乎慢两倍。当涉及到删除它甚至变得更糟糕:当保存在所有实体删除结束时,DbContext比ObjectContext慢18倍。

When using DbContext in a database-first scenario I found out that adding and deleting entities is very slow compared to ObjectContext. If adding 2000 entities and saving the changes at the end, DbContext is 3 to 5 times slower than ObjectContext (btw.: I know that adding a large amount of entities would be better using SqlBulkCopy but that's not the point). If saving changes after each addition DbContext is still nearly two times slower. When it comes to deletion it even gets worse: When saving at the end of all entity removals, DbContext is around 18 times slower than ObjectContext.

我使用了高度开发的测试应用程序,用于比较数据库访问技术和小型控制台应用程序进行双重检查。两者都显示使用DbContext添加和删除实体的不良结果。以下是控制台应用程序的结果:

I took my highly developed test application I use to compare database access technologies and a small console application to double check. Both showed bad results for adding and deleting entities using DbContext. Here are the results of the console application:

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

我尝试在VC 2010中使用EF 4.3和VS 11中的EF 5.0 Beta 2,结果几乎相同。我使用了EF 4.x POCO Entity Generator for C#提供的T4模板,用于C#的EF 4.x DbContext生成器和用于C#的EF 5.x DbContext生成器。

I tried using EF 4.3 in VC 2010 and EF 5.0 Beta 2 in VS 11 with nearly same results. I used the T4 templates provided by 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#".

可能是错的?根据测试结果,我永远不会在应用程序中使用DbContext,该应用程序必须添加或删除实体(使DbContext不幸对我无法使用)。

What could be wrong? According to the test results I would never use DbContext in an application that has to add or delete entities (what makes DbContext unfortunately unusable for me).

我把控制台测试我的Web服务器上的应用程序: EF 4.3 DbContext测试 EF 5.0 DbContext Test

I put the console test applications on my web server: EF 4.3 DbContext Test, EF 5.0 DbContext Test

任何想法/修正是赞赏。

Any ideas/corrections are appreciated.

推荐答案

尝试将其添加到您的DbContext测试中:

Try to add this to your DbContext tests:

dbContext.Configuration.AutoDetectChangesEnabled = false;

// Now do all your changes

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

并尝试再次运行测试。

DbContext API中有一些架构更改,每次您在添加附加删除上下文中的任何内容。在ObjectContext API中,只有当您触发 SaveChanges 时,此检测才会运行。它是大多数常见情况的更好的解决方案,但需要对大量数据处理进行特殊处理。

There was some architectural change in DbContext API which checks changes in entities every time you Add, Attach or Delete anything from the context. In ObjectContext API this detection run only when you triggered SaveChanges. It is better solution for most common scenarios but it requires special handling for mass data processing.

这篇关于DbContext在添加和删除时非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆