实体框架4非常慢地将订单链接到具有10,000个订单的客户 [英] Entity Framework 4 very slow linking an Order to Customer who has 10,000 orders

查看:124
本文介绍了实体框架4非常慢地将订单链接到具有10,000个订单的客户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个客户和订单实体。

This one has me stumped.

客户可以有多个订单。

当我创建新订单并设置客户属性(Order.Customer = customer)时,客户有10,000个订单,那么有一个LONG延迟(20s秒)。看来,在添加新的订单之前,上下文正在加载所有10,000个订单。

Where the Customer has 10,000 orders when I create a new Order and set the Customer property (Order.Customer = customer) there is a LONG delay (20s of seconds). It appears that the context is loading all 10,000 orders before adding this new one.

我目前没有直接使用我怀疑可能会帮助的FK。

I am not currently using FKs directly which I suspect may help.

任何想法如何可以改善没有大规模重构的事情?

Any ideas how I can improve matters without a massive refactor?

干杯。

推荐答案

问题很可能是您使用的是T4 POCO模板。此模板生成讨厌的修复方法,并在所有导航属性内部使用它们。如果您在一侧修改导航属性,则会触发fixup,这将尝试修改反向导航属性以使对象图一致。这里有问题。将客户属性分配给订单实例后,它将修复订单实例中的c>属性,但将其作为任何其他代码修复访问属性,并触发延迟加载所有客户的订单。

The problem is most probably that you are using T4 POCO template. This template generates nasty fixup methods and use them internally in all navigation properties. If you modify navigation property on one side it triggers fixup which will try to modify reverse navigation property to make the object graph consistent. And here comes the problem. Once you assign Customer property to the Order instance it will fixup Orders property on the Customer instance but fixup access property as any other code and triggers lazy loading of all customer's orders.

只有几个解决方案:


  • 使用外键关系船和设置FK属性。这应该适用于插入,但是为了更新,它仍然可能会导致问题,因为将FK属性设置为另一个值将将 null 设置为导航属性,并将再次触发上一个父代的修复。 / li>
  • 关闭此操作的延迟加载 - 您很可能不需要创建新的订单。

  • 修改模板并删除fixupus

  • Use Foreign key relation ship and set FK property. That should work for inserts but for update it can still cause problems because setting FK property to another value will set null to navigation property and that will again trigger fixup for previous parent.
  • Turn off lazy loading for this operation - you most probably doesn't need it to create new order.
  • Modify template and remove fixupus

这篇关于实体框架4非常慢地将订单链接到具有10,000个订单的客户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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