Preventing Eager Loading
Assume a data model where there is a Customer -> Order (1:M) relationship. In my app, when I retrieve a Customer object and return it to the client (n-tier), L2S is automatically including all associated Order objects with the Customer object. Conversely, when I retrieve an Order object, L2S is automatically retrieving the associated Customer object. I believe this is called "eager loading". There may be cases where I don't want this eager loading to occur. How can I prevent it in cases where I don't want this to happen?
Thanks.
Answers
Hi Randy,
Damien’s suggestion is really helpful! To add some supplements, can you set the DataContext.DeferredLoadingEnabled to false to turn off the deferred loading? Then if any relationships that you want to load, you can use DataLoadOptions.LoadWith to load them.
If you have any questions, please feel free to let me know.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byDavid DeWinter - MSFTAnswererWednesday, November 11, 2009 6:40 PM
All Replies
- It's not the eager loading that is causing this but most likely the serialization code is enumerating the collection which is causing it to load.[)amien
- Damien,
Thanks for the reply but this doesn't really help me. I don't want the child loading to occur, at all. How do I prevent this?
Randy - If you don't want to serialize the orders when serializing the customer you need to effectively remove the orders navigation.You could either delete it via the designer if you are not actually using it anywhere (it won't affect FK's in the underlying database) or set the property to internal - I believe the serialization code will then leave it alone and your code can continue to use it.[)amien
- So there is no run-time option to turn this on or off? It sounds like I need to decide this at compile time. But there might be cases in our app when we want the orders records and cases when we don't. Why should we be forced to always retrieve them when we don't need them, or never retrieve them?
Randy - If you want to sometimes serialize some elements and at other times not serialize them then you need to change the serializer - LINQ to SQL isn't doing anything other than retrieving navigation properties when they are accessed.Your serializer is accessing them. LINQ to SQL has no way of telling the serializer it can't/shouldn't have them as such a mechanism does not exist in .NET's serialization APIs.[)amien
Hi Randy,
Damien’s suggestion is really helpful! To add some supplements, can you set the DataContext.DeferredLoadingEnabled to false to turn off the deferred loading? Then if any relationships that you want to load, you can use DataLoadOptions.LoadWith to load them.
If you have any questions, please feel free to let me know.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marked As Answer byDavid DeWinter - MSFTAnswererWednesday, November 11, 2009 6:40 PM


