EF缓存不同DbContext实例之间的实体? [英] Does EF caches entities between different instances of DbContext?

查看:109
本文介绍了EF缓存不同DbContext实例之间的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Asp.net中,每个查询创建DbContext会使EF只读取其缓存中的数据,还是每次查询DB?我知道每个AppDomain的元数据缓存,但只是数据呢?



上下文:使用MVC4 + Web API前端的数据采集和可视化应用程序,不会称之为高容量,但是很多查询在一些较短的时间内返回相同的数据集。

解决方案

实体框架不每个AppDomain都有一个数据缓存,每个上下文实例只有一个缓存。



如果您为每个请求或查询创建一个新的上下文,则以空缓存开始,EF将从数据库中获取数据。


$此外,术语每个上下文实例的缓存可能是误导的,因为这并不意味着如果实体已经加载到上下文高速缓存中,则EF不会向数据库运行查询。如何使用这种缓存的方式(或不是)如下:




  • / strong>在 DbSet< T> 或通常在 IQueryable< T> 上的LINQ到实体查询将运行数据库查询,无论实体是否已经存在于上下文中。但是如果具有查询实体的密钥相同的实体已经存在于上下文EF中,则会将该查询的结果抛出,并将缓存的实体实例返回给调用者。



    它执行此检查,如果具有相同键的实体在之后已经运行查询。 (对于复杂的查询 - 例如包含 Include 的查询,它不能执行此检查,因为它不知道将返回哪些实体和键值。)



    这是默认行为( MergeOption AppendOnly )。相信,您可以将此行为更改为 OverwriteChanges 和其他选项,但并不会阻止LINQ查询始终发出数据库查询。


  • 只需按键,就可以使用 GetObjectByKey 查找(使用 DbContext ),它将首先检查具有该键的实体是否已经在上下文中缓存,然后返回此缓存对象。如果不是,它将运行一个数据库查询来加载它。


  • 您可以查询EF的ChangeTracker,它特别支持 DbContext 您可以通过 DbSet< T> .Local 集合访问上下文缓存。



    这里的问题是,如果 Local 上的查询未返回结果,则无需自动查询数据库的逻辑。你必须手动写这个逻辑。更大的问题是,对 Local 的查询是LINQ对对象,而不是LINQ到实体(本地不执行 IQueryable< T> ,只有 IEnumerable< T> ),所以你经常要重写你的在本地中执行的查询 - 例如,您不能在此使用 Include ,您不能使用任何 EntityFunctions ,您将获得与区分大小写等字符串比较的不同行为。



Does creating DbContext per query in Asp.net make EF only read the data from its cache, or does it query DB for the whole sets every time? I know about metadata caching per AppDomain, but what about just the data?

Context: data acquisition and visualisation application with MVC4 + Web API frontend, wouldn't call that "high volume", but lots of queries return the same sets of data in some shorter time frame.

解决方案

Entity Framework doesn't have a data cache per AppDomain, only a cache per context instance.

If you create a new context per request or query you start with an empty cache and EF will fetch the data from the database.

Moreover, the term "cache per context instance" can be misleading as it doesn't mean that EF won't run queries to the database if the entities are already loaded in the context cache. The way how this cache works and how you can leverage it (or not) is the following:

  • Every LINQ-to-Entities query on a DbSet<T> or generally on an IQueryable<T> will run a database query, no matter if the entities already exist in the context or not. But if an entity with the same key as a queried entity already exists in the context EF will throw the result of that query away and return the cached entity instance back to the caller.

    It does this check if the entity with the same key exists after it has run the query. (For complex queries - for example queries that contain an Include - it can't do this check before because it cannot know which entities and key values will be returned.)

    That's the default behaviour (MergeOption is AppendOnly). You can change this behaviour to OverwriteChanges and other options, I believe, but none of them will avoid that LINQ queries always issue database queries.

  • For querying an entity just by its key you can use GetObjectByKey or Find (with DbContext) which will check first if the entity with that key is already cached in the context and then return this cached object. If not it will run a database query to load it.

  • You can query EF's ChangeTracker, it's especially well supported with DbContext where you have access to the context cache via the DbSet<T>.Local collection.

    The problem here is that there is no logic to query the database automatically if a query on Local does not return a result. You have to write this logic manually. The even bigger problem is that a query on Local is LINQ-to-Objects and not LINQ-to-Entities (Local doesn't implement IQueryable<T>, only IEnumerable<T>), so you often have to rewrite your queries to act on Local - for example you can't use Include here, your can't use any EntityFunctions, you will get different behaviour for string comparisons regarding case sensitivity, etc., etc.

这篇关于EF缓存不同DbContext实例之间的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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