Ask a questionAsk a question
 

AnswerPreventing Eager Loading

  • Saturday, November 07, 2009 2:26 AMRandy Minder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    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

All Replies

  • Saturday, November 07, 2009 5:09 AMDamien Guard - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Saturday, November 07, 2009 11:49 AMRandy Minder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Saturday, November 07, 2009 9:07 PMDamien Guard - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Saturday, November 07, 2009 9:11 PMRandy Minder Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Sunday, November 08, 2009 12:13 AMDamien Guard - MSFTModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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
  • Monday, November 09, 2009 2:00 AMLingzhi SunMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    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 Sun

    MSDN 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.