English 中文(简体)
帮助我了解实体框架4
原标题:Help me understand entity framework 4 caching for lazy loading

我正在得到与实体框架4.0的一些意想不到的行为,我希望有人能够帮助我理解这一点。 为解决这一问题,我正在利用北风数据库。 我也正在使用默认代码生成器(不是波科或自我跟踪)。 我期望,在我没有把这些物体分开的情况下,我询问框架只进行一轮访问的背景。 如果我放弃zy装,我就这样做了。 目前,在我的申请中,我正直地转而处理zy问题,然后将其 back倒,以便我能找到理想的行为。 这远远不错,因此请提供帮助。 这里是一个能够表明我问题的好榜样。

Public Sub ManyRoundTrips()
    context.ContextOptions.LazyLoadingEnabled = True
    Dim employees As List(Of Employee) = context.Employees.Execute(System.Data.Objects.MergeOption.AppendOnly).ToList()

     makes unnessesary round trip to the database, I just loaded the employees 
    MessageBox.Show(context.Employees.Where(Function(x) x.EmployeeID < 10).ToList().Count)
    context.Orders.Execute(System.Data.Objects.MergeOption.AppendOnly)
    For Each emp As Employee In employees
         makes unnessesary trip to database every time despite orders being pre loaded. 
        Dim i As Integer = emp.Orders.Count
    Next
End Sub

Public Sub OneRoundTrip()
    context.ContextOptions.LazyLoadingEnabled = True
    Dim employees As List(Of Employee) = context.Employees.Include("Orders").Execute(System.Data.Objects.MergeOption.AppendOnly).ToList()

    MessageBox.Show(employees.Where(Function(x) x.EmployeeID < 10).ToList().Count)

    For Each emp As Employee In employees
        Dim i As Integer = emp.Orders.Count
    Next
End Sub

为什么是进行无常的回程旅行的法典的首批部分?

问题回答

你的期望是不正确的。 Queries always query the DB. 途径。 这是因为准则总是被转换为Q。

如果物体已经过期,则从该行装载,如果该物体有点,则使用,ObjectContext.GetObjectByKey()

第一次不必要的旅行是必要的——你进行了新的询问,数据库在此期间可能已经发生变化。 如果你使用雇员变量(如果你储存了询问的result),那么就不必去数据库。

第二是必需的,因为你要求它为每个雇员发出命令。 在拉齐装载和没有包裹的情况下,在你要求时,它就没有读过命令。 Orders.Count().

认为直到你开始盘问(或说某种方法要求它加以篡改)为止,LINQ至EF没有任何东西。 如果你在变数中避开了问询,然后打电话到Count(),那将进行回程。 如果你使用同样的询问和开始计算,将再进行一次回程。 如果该询问所涉实体本身有关系,而且每当你接触时,就会再进行一次回程。

你的第二种例子表明,如果你事先知道你想要的是这些命令,那么如何行使这一权利,Ager /em>。 通知你不要回头再向雇员提问,你重复你已经装满的。





相关问题
Write-though caching of large data sets in WCF?

We ve got a smart client that talks to a SQL Server database via WCF, displaying the entities in the database, and allowing the user to edit those entities. Some of the WCF calls return a large data ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

how to tell clicking "back" to load cache?

I would like for my site when someone clicks "Back" or "Forward" for the server to tell the browser to load the cache instead of reloading the entire page. I ve tested some headers and done research, ...

java plugin cache and dynamic IP host

I m trying to use Amazon S3 and Amazon Cloudfront CDN to deliver the jar files of my applet application. I m seeing several cache misses of my jars by the java plugin. This is a show-stopper for me, ...

Frequently Used metadata Hashmap

Are there any implementations of a static size hashtable that limits the entries to either the most recently or most frequently used metadata? I would prefer not to keep track of this information ...

PHP - Memcache - HTML Caching

I would like to create a caching system that will bypass some mechanisms in order to improve the performance. I have some examples: 1-) I have a dynamic PHP page that is updated every hour. The page ...

Performance of Sql subqueriesfunctions

I am currently working on a particularly complex use-case. Simplifying below :) First, a client record has a many-to-one relationship with a collection of services, that is, a single client may have ...