This one has me stumped.
I have a Customer and Order entity. the Customer can have many Orders.
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.
I am not currently using FKs directly which I suspect may help.
Any ideas how I can improve matters without a massive refactor?
Cheers.
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:
null
to navigation property and that will again trigger fixup for previous parent.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With