locked
deferred loading example is incorrect RRS feed

  • Question

  • I think this example on msdn regarding deferred loading is not quite correct
    http://msdn.microsoft.com/en-us/library/dd456846(VS.100).aspx

    it does this
    var db = new AdventureWorksEntities()
    db.Options.DeferredLoadingEnabled = true;
    customer.SalesOrderHeader.Attach(customer.SalesOrderHeader.CreateSourceQuery().Take(5));
    foreach (SalesOrderHeader order in customer.SalesOrderHeader)
     {

    }
    Attach does not set IsLoaded to true so when u loop through Customer.SalesOrderHeader collection with deferred loading turned, it would make a database call and would cause the SalesHeaderOrder to contain more then 5 records which teh take does.

    Zeeshan Hirani
    Monday, May 25, 2009 5:07 AM

Answers

  • Hello Zeeshan,

    Thanks for the feedback! Looking at the example, I can see a few things that can be improved about it, and I will pass this feedback to the writers.

    You are correct that Attach will not affect the IsLoaded flag, and that therefore the foreach will trigger loading the collection again from the database. I can see that the point that the example is trying to make (that SaleOrderDetails are loaded lazily) is somewhat obscured by the use of the source query and Attach.

    As a side note, this won't cause duplicates on the collection: identity resolution will make sure that objects coming from the query triggered by lazy loading will be merged with objects already tracked according to the rules of MergeOption.AppendOnly.

    Thanks again,
    Diego


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Monday, May 25, 2009 8:24 AM
    Moderator