EF4和与AddObject相关的收集不期望的加载 [英] EF4 and undesired loading of related collection with AddObject

查看:106
本文介绍了EF4和与AddObject相关的收集不期望的加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的情况,添加记录导致不必要的加载相关集合。

I have an odd case where adding a record is causing unwanted loading of a related collection.

例如,我有请求和会话。会话可以包含许多请求。我已经加载了会话,只是想添加一个新的请求。

For example, I have Requests and Sessions. A Session can contain many Requests. I already have loaded the session, and just want to add a new request.

但是,当我在Request存储库的ObjectSet上设置调用AddObject时,SQL Profiler会显示一个选择查询在该会话的所有相关请求上执行。

However, when I set call AddObject on the ObjectSet of the Request repository, SQL Profiler shows a select query getting executed on all related requests on that session.

以下是问题代码:

this._request = new Request
                    {
                        Action = (string)filterContext.RouteData.Values["action"],
                        Controller = filterContext.Controller.GetType().Name,
                        DateAdded = userContext.Session.DateLastActive,
                        IpAddress = filterContext.HttpContext.Request.UserHostAddress,
                        SessionId = userContext.Session.Id
                    };

loggingService.AddRequest(this._request);

最后一行只是调用我的服务,反过来,只需调用 _objectSet.AddObject(entity)

That last line just calls into my service, which in turn, just calls _objectSet.AddObject(entity).

同样的事情会发生,如果我尝试设置新的请求的 Session = userContext .Session (而不是上述 SessionId = userContext.Session.Id ) - 查询将在设置此属性而不是AddObject时执行。所以看来,EF4认为在某些原因引用的时候它需要Session的相关请求。

The same thing would happen if I tried setting the new Request's Session = userContext.Session (instead of the above SessionId = userContext.Session.Id) -- the query would execute on setting this property, instead of on AddObject. So it's seems EF4 thinks it needs the related Request collection on the Session when it's referenced for some reason.

但是我不需要会话上的关联请求被使用或引用。所以我不知道为什么EF4加载它们。我逐步浏览了代码,并验证了这一点恰好位于 AddObject(entity)行(Profiler显示在同一个实例中执行的查询)。

But I don't need the associated requests on the session, nor are the being used or referenced. So I'm not sure why EF4 is loading them. I stepped through the code, and verified that this happens exactly on the AddObject(entity) line (Profiler shows the query getting executed at the same instance).

为什么会发生这种情况,我该如何阻止?

Why would this happen, and how can I stop it?

提前感谢

编辑:这是因为EF4正试图将新的请求添加到相关Session.Requests收集,并检索所有其他的?如果是这样,有什么办法可以预防吗?就像我说的,我不需要这些请求,我只需要添加它并继续。

Is this because EF4 is trying to add the new Request to the related Session.Requests collection, and goes and retrieves all of the other ones too? If so, is there any way to prevent that? Like I said, I don't need those Requests, I just need to add it and move on.

推荐答案

我想你正在使用POCO T4模板,该模板怀疑这种行为。问题是由POCO模板生成的fixup方法。每次分配导航属性,外键或添加对象到相关对象的集合时,这些方法执行对象图修正。这意味着他们更新相关实体的导航。在你的情况下,这意味着fixup方法将请求添加到会话请求 code>。访问收集将触发延迟加载。避免这种情况的唯一方法是:

I guess you are using POCO T4 template which suspects exactly this behavior. The problem are fixup methods generated by POCO template. Every time you assign navigation property, foreign key or add object to collection of related objects these methods performs object graph fixup. It means they update navigation on related entity as well. In your scenario it means that fixup methods adds Request to Requests collection in the Session. Access to the collection will trigger lazy loading. The only ways to avoid this are:


  • 关闭此操作的延迟加载( context.ContextOptions.LazyLoadingEnabled =

  • 会话中删除请求属性

  • 修改T4模板并删除修复方法

  • 修改T4模板并从请求中删除虚拟机属性(会话不支持延迟加载)

  • Turn off lazy loading for this operation (context.ContextOptions.LazyLoadingEnabled = false)
  • Remove Requests property from Session
  • Modify T4 template and remove fixup methods
  • Modify T4 template and remove virtual from Requests property (Session will not support lazy loading)

这篇关于EF4和与AddObject相关的收集不期望的加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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