实体框架中的AsNoTracking() [英] AsNoTracking() in entityframework

查看:89
本文介绍了实体框架中的AsNoTracking()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 AsNoTracking(),并且知道在使用第一级缓存时会禁用它。但是如何使用 AsNoTracking()来提高性能?

I use AsNoTracking() and know that the first-level caching is disabled when using it. But how can using AsNoTracking() improve the performance? What is the benefit of using it?

推荐答案

AsNoTracking()意味着什么?实体将不会由ObjectContext实例本地缓存。这有一些实际的好处:

AsNoTracking() means that the entities will not be cached locally by the ObjectContext instance. This has a few practical benefits:


  1. 内存使用量:由于ObjectContext没有引用实体归还给您后,垃圾收集器可以在您不再引用它们时立即将它们删除。通常,需要先处理ObjectContext。

  1. Memory Usage: Since the ObjectContext isn't referencing the entities after they're returned to you, the Garbage Collector can get rid of them as soon as you're no longer referencing them. Normally, the ObjectContext would need to be disposed before this could happen.

性能:由于EF不必尝试将数据库中返回的每个记录与身份映射中的本地实体进行匹配,您的查询的执行速度可能会稍快。

Performance: Since EF doesn't have to try to match every record returned from the database with a local entity in the identity map, your queries might execute slightly faster.

货币:由于查询返回的结果是直接从数据库结果中实现的实体,并且不依赖于本地缓存,因此返回的实体应始终反映数据库中的最新值。

Currency: Since queries return entities materialized directly from the database results and do not rely on a local cache, the returned entities should always reflect the latest values in the database.

无状态:由于没有使用ObjectContext跟踪实体,因此您可以继续将相同的ObjectContext实例无限期地用于只读查询,并且不必因存在短暂的ObjectContext而受到一般建议的约束。

Statelessness: Since the entities are not being tracked by ObjectContext, you can continue to use the same ObjectContext instance indefinitely for read-only queries and need not feel constrained by the normal advice regarding having short-lived ObjectContexts.

AsNoTracking()是个好主意仅查询实体。如果您需要更新它们,则无法使用,那就是权衡。

AsNoTracking() is a good idea if you're only querying entities. It won't work if you need to update them, that is the tradeoff.

这篇关于实体框架中的AsNoTracking()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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