[英]EF6 LazyLoadingEnabled enabled but loads related objects
I use EF 6 with my database. 我在数据库中使用EF 6。 I call constructor ctx=new EntityContext().
我称构造函数ctx = new EntityContext()。 Then try to get ctx.Set() for example.
然后尝试获取ctx.Set()例如。 LazyLoadingEnabled = true by default.
默认情况下,LazyLoadingEnabled = true。 But method returns collection with included related objects.
但是方法返回包含相关对象的集合。 More over, it looks like objects are recursively looped.
而且,看起来对象是递归循环的。 What am I done wrong?
我做错了什么? Why lazy loading isn't work?
为什么延迟加载不起作用? I also start SQL profiler, I'm not much familiar with it, but... I start monitoring in profiler, then start test application and... the only request I saw is request to my table and no other requests to related tables.
我还启动了SQL事件探查器,对此我不太熟悉,但是...我开始在事件探查器中进行监视,然后启动测试应用程序,并且...我看到的唯一请求是对表的请求,而对相关表没有其他请求。 Request looks like:
请求看起来像:
SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
[Extent1].[CreateDate] AS [CreateDate],
[Extent1].[Type] AS [Type],
[Extent1].[Status] AS [Status]
FROM [dbo].[Task] AS [Extent1]
thats all, but debugger show entity with related objects. 多数民众赞成,但调试器显示实体与相关的对象。 Why so?
为什么这样?
I believe the issue here is not EF loading the related object, but YOU loading them! 我相信这里的问题不是EF加载相关对象,而是您加载它们!
If you're looking at a collection in the debugger in VS, then you expand a collection, EF will go and query the database for the related collection (the same as if you did it in code). 如果您正在VS中的调试器中查看一个集合,那么您将展开一个集合,EF将前往数据库中查询相关集合(就像在代码中一样)。
If you watch SQL profiler when you do this, you should see it go and perform a second query at that point. 如果您在执行此操作时观看了SQL事件探查器,则应该看到它,然后再执行第二次查询。
EF works via dynamic proxies, and expanding a collection in the debugger is the equivalent of working with the object in code, and calling MyObject.TheCollectionProperty.ToList()
EF通过动态代理工作,并且在调试器中扩展集合等效于使用代码中的对象并调用
MyObject.TheCollectionProperty.ToList()
Lazy loading just means that the SQL needed to populate the related collection isn't going to be executed unless you try to access the related collection (as opposed to a single up front query with a join). 延迟加载仅意味着填充相关集合所需的SQL不会执行,除非您尝试访问相关集合(与使用联接的单个前期查询相反)。 Sometimes this is desirable, sometimes this isn't.
有时这是可取的,有时则不是。 It will depend on how your application logic wants to work.
这将取决于您的应用程序逻辑如何工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.