Entity Framework 4 very slow linking an Order to Customer who has 10,000 orders

点点圈 提交于 2019-12-05 14:37:44

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.

There are only few solutions:

  • 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

Maybe a different approach might work. When you have a customer instance try:

customer.Orders.Add(new Order(){parameter1 = value1, parameter2=valu2, etc.})

Am not @work right now so don't have any code by hand to check entity structure (work with Self-tracking entities for a project) to see if this makes sense. But by adding it to the collection of Orders the entity might resolve the relation between order and customer without getting all the other 10k orders of this specific customer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!