none
Lazy loading loading related entitites without navigation accessor access RRS feed

  • Question

  • Using the EF 4.2 with VS10. 

    I  Have a master and detail table with relationship.

    I'm using the edmx model, so lazy loading is the default.

    If I do

      var q = from a in dc.DetailTable select a;

    I would have expected that only the detail table would be loaded.  However,  a SQL trace shows the related master table is being loaded as well.

    I didn't think the master table would be loaded since no navigation property was accessed.

    I thought this since I read the documentation at:  http://msdn.microsoft.com/en-us/library/bb896272.aspx

     



    • Edited by chuck02323 Wednesday, January 25, 2012 11:29 PM
    Wednesday, January 25, 2012 11:20 PM

Answers

  • Hi,

    I don' t think such a query on the detail table will access the master table here.  EF 4.2 does not change its the default lazy loading settings here. 

    Please first check whether the LazyLoadingEnabled property is true: for DbContext, its context.Configuration.LazyLoadingEnabled and for ObjectContext, it's ObjectContext.ContextOptions.LazyLoadingEnabled. 

    Did you do some databinding on the master and detail table?   Maybe it's the databinding logic access the master table navigation properties.  Besides, please provide us with the SQL trace you mentioned.  Is the SQL using JOIN or it loads the master table object one by one? 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 26, 2012 5:02 AM
    Moderator
  • Hi,

    Thanks for following up!  Based on the single row select queries you mentioned, I think the problem is that the databinding logic access the master navigation property.  Could you please provide us with more detailed information about how you use the query?  

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 27, 2012 11:06 AM
    Moderator

All replies

  • Hi,

    I don' t think such a query on the detail table will access the master table here.  EF 4.2 does not change its the default lazy loading settings here. 

    Please first check whether the LazyLoadingEnabled property is true: for DbContext, its context.Configuration.LazyLoadingEnabled and for ObjectContext, it's ObjectContext.ContextOptions.LazyLoadingEnabled. 

    Did you do some databinding on the master and detail table?   Maybe it's the databinding logic access the master table navigation properties.  Besides, please provide us with the SQL trace you mentioned.  Is the SQL using JOIN or it loads the master table object one by one? 

    Good day!

    Thanks


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Thursday, January 26, 2012 5:02 AM
    Moderator
  • I checked the ContextOptions and LazyLoading was set.

    My sql query is just selecting from the detail table.    var q = from a in dc.DetailTable select a;

    The trace shows the detail select and then a few hundred single row selects for the Master table.   (Select * from master where ID = 2)

    In the SQL DB their is a Foreign Key on the detail table.  In the edmx file this navigation relationship was created when the tables were added to the diagram.  The properties were not modified from the default.

    The query is being executed in an MVC app, the edmx file is in a separate buis dll project which is referenced.

     

    Thursday, January 26, 2012 11:14 PM
  • Hi,

    Thanks for following up!  Based on the single row select queries you mentioned, I think the problem is that the databinding logic access the master navigation property.  Could you please provide us with more detailed information about how you use the query?  

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us
    Friday, January 27, 2012 11:06 AM
    Moderator