DbContext ChangeTracking是否会降低性能? [英] DbContext ChangeTracking kills performance?

查看:106
本文介绍了DbContext ChangeTracking是否会降低性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将应用程序从EF1升级到EF4.1 我使用"ADO.NET DbContext Generator"模板创建了一个DbContext和一组POCO.

I am in the process of upgrading an application from EF1 to EF4.1 I created a DbContext and a set of POCOs using the "ADO.NET DbContext Generator" templates.

当我查询生成的DbContext时,查询的数据库部分需要4毫秒的时间来执行(通过EF Profiler验证).然后,它需要大约40秒的时间(换句话说就是:FORTY!)来完成将结果返回给应用程序的所有操作.

When I query the generated DbContext the database part of the query takes 4ms to execute (validated with EF Profiler). And then it takes the context about 40 seconds (in words: FORTY!) to do whatever it does before it returns the result to the application.

EF1可以在不到2秒的时间内处理相同的查询.

EF1 handles the same query in less than 2 seconds.

关闭AutoDetectChanges,LazyLoading和ProxyGeneration可以使我赢得2-3秒.

Turning off AutoDetectChanges, LazyLoading and ProxyGeneration wins me 2-3 seconds.

当我使用AsNoTracking()扩展方法时,我可以将总执行时间减少到大约3秒.

When I use the AsNoTracking() extension method I am able to reduce the total execution time to about 3 seconds.

这表明ChangeTracking是罪魁祸首.

That indicates that ChangeTracking is the culprit.

但是我需要ChangeTracking.我必须能够最终保留所有更改,而不必手动选择修改了哪些实体.

But ChangeTracking is what I need. I must be able to eventually persist all changes without having to handpick which entities were modified.

有什么想法可以解决该性能问题吗?

Any ideas how I could solve that performance issue?

推荐答案

此技巧是无需跟踪 .易变的项目都无需检查就可以写出来(一个弱点……可能会接受也可能不会接受).

Is the technique at the end of this documentation useful? Alternatively, I've avoided many of the performance pitfalls using a fluent interface to declaratively state which entities in a given transaction for sure won't change vs. might change (immutable vs. immutable). For example, if the entities I am saving are aggregate roots in which the root or its entities refer to "refdata" items, then this heuristic prevents many writes because the immutable items don't need to be tracked. The mutable items all get written without check (a weakness... One which may or may not be acceptable).

我正将其与通用存储库模式一起使用,正是因为我不想跟踪每种情况的更改或实施特定策略.如果那还不够,也许可以在上下文之外滚动您自己的更改跟踪并根据需要添加实体.

I'm using this with a generic repository pattern precisely because I don't want to track changes or implement a specific strategy for each case. If that's not enough, perhaps rolling your own change tracking outside of the context and adding entities in as needed will work.

这篇关于DbContext ChangeTracking是否会降低性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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