简体   繁体   English

在实体框架中添加AsNoTracking会影响计数吗?

[英]Does adding AsNoTracking in Entity Framework impact a Count?

Does adding AsNotracking function to a count in Entity Framework 6 has an impact on a count? 向实体框架6中的计数添加AsNotracking功能是否会影响计数? More specifically does it improve or decrease performance or will the count result get cached? 更具体地说,它是改善还是降低性能还是计数结果会被缓存?

With AsNoTracking 使用AsNoTracking

myContext.Products.AsNoTracking().Count();

Without AsNoTracking 没有AsNoTracking

myContext.Products.Count();

Since no entities are being generated with this query, there is nothing to track, therefore no difference in performance. 由于没有使用此查询生成任何实体,因此无需跟踪,因此性能没有差异。 The SQL generated from this would look something like this: 从中生成的SQL看起来像这样:

SELECT COUNT(1)
FROM [dbo].[Products]

which obviously returns a single row. 这显然会返回一行。 If it was creating entities to be tracked, it would have to return every single entity in that table. 如果要创建要跟踪的实体,则必须返回该表中的每个实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM