与 ObjectContext 相比,为什么在 EF 4.1 中插入实体如此缓慢? [英] Why is inserting entities in EF 4.1 so slow compared to ObjectContext?

查看:16
本文介绍了与 ObjectContext 相比,为什么在 EF 4.1 中插入实体如此缓慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我在一个事务中插入 35000 个对象:

Basically, I insert 35000 objects within one transaction:

using(var uow = new MyContext()){
  for(int i = 1; i < 35000; i++) {
     var o = new MyObject()...;
     uow.MySet.Add(o);
  }
  uow.SaveChanges();
}

这需要永远!如果我使用底层 ObjectContext(通过使用 IObjectAdapter),它仍然很慢,但需要大约 20 秒.看起来 DbSet<> 正在做一些线性搜索,这需要平方的时间......

This takes forever! If I use the underlying ObjectContext (by using IObjectAdapter), it's still slow but takes around 20s. It looks like DbSet<> is doing some linear searches, which takes square amount of time...

还有其他人看到这个问题吗?

Anyone else seeing this problem?

推荐答案

正如 Ladislav 在评论中已经指出的,您需要禁用自动更改检测以提高性能:

As already indicated by Ladislav in the comment, you need to disable automatic change detection to improve performance:

context.Configuration.AutoDetectChangesEnabled = false;

默认情况下在 DbContext API 中启用此更改检测.

This change detection is enabled by default in the DbContext API.

DbContext 的行为与 ObjectContext API 如此不同的原因是 DbContext API 的更多函数将调用 DetectChanges 在启用自动更改检测时在内部比 ObjectContext API 的函数.

The reason why DbContext behaves so different from the ObjectContext API is that many more functions of the DbContext API will call DetectChanges internally than functions of the ObjectContext API when automatic change detection is enabled.

此处您可以找到默认调用 DetectChanges 的那些函数的列表.它们是:

Here you can find a list of those functions which call DetectChanges by default. They are:

  • AddAttachFindLocalRemove 成员DbSet
  • DbContext
  • 上的 GetValidationErrorsEntrySaveChanges 成员
  • DbChangeTracker
  • 上的 Entries 方法
  • The Add, Attach, Find, Local, or Remove members on DbSet
  • The GetValidationErrors, Entry, or SaveChanges members on DbContext
  • The Entries method on DbChangeTracker

特别是 Add 调用了 DetectChanges,这会导致您遇到的性能不佳.

Especially Add calls DetectChanges which is responsible for the poor performance you experienced.

我与此形成对比,ObjectContext API 仅在 SaveChanges 中自动调用 DetectChanges 而不是在 AddObject 和上面提到的其他相应的方法.这就是 ObjectContext默认性能更快的原因.

I contrast to this the ObjectContext API calls DetectChanges only automatically in SaveChanges but not in AddObject and the other corresponding methods mentioned above. That's the reason why the default performance of ObjectContext is faster.

他们为什么在这么多函数的DbContext中引入了这个默认的自动变化检测?我不确定,但似乎禁用它并在适当的点手动调用 DetectChanges 被视为 高级并且可以轻松地将细微的错误引入您的应用程序,因此请谨慎使用 [it].

Why did they introduce this default automatic change detection in DbContext in so many functions? I am not sure, but it seems that disabling it and calling DetectChanges manually at the proper points is considered as advanced and can easily introduce subtle bugs into your application so use [it] with care.

这篇关于与 ObjectContext 相比,为什么在 EF 4.1 中插入实体如此缓慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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