Print Page | Close Window

Lazy Loading

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3706
Printed Date: 26-Oct-2025 at 1:05am


Topic: Lazy Loading
Posted By: orcities
Subject: Lazy Loading
Date Posted: 10-Oct-2012 at 8:20am
I am building a WinRt app. I have a Web Server component storing my DataModel and a seperate winrt library project holding the winrt version of the app, similar to how silverlight is done.

I noticed that when I set my models LazyLoading property to false it still lazy loads. I have tried rebuilding the project as well as restarting iis. Nothing changes the outcome. I do not want lazy loading.



Replies:
Posted By: kimj
Date Posted: 10-Oct-2012 at 8:33am
My guess is you're setting the "Lazy Loading Enabled" property within the EDM Designer.   DevForce doesn't actually use this setting, it's used by EF in an EF only model.  Confusing, I know, but this was true in DevForce 2010 as well.
 
The correct way to turn lazy loading within DevForce off is via the EntityReferenceStrategy.  More here - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/navigation-properties-data-retrieval - http://drc.ideablade.com/devforce-2012/bin/view/Documentation/navigation-properties-data-retrieval . 


Posted By: orcities
Date Posted: 10-Oct-2012 at 8:47am
Thank you for this response. This fixed the lazy loading issue but now when I do the following the Included properties don't load:

return await EntityManager.ContactEmails
                .Where(e => e.InActive == false && e.ContactId == pContactId)
                .Include(e => e.Email)
                .ExecuteAsync();

EntityManager.DefaultEntityReferenceStrategy = new EntityReferenceStrategy(EntityReferenceLoadStrategy.Load, MergeStrategy.OverwriteChanges, true);


Posted By: orcities
Date Posted: 10-Oct-2012 at 8:54am
Also, even when I set to load from the datasource everytime  a specific instance still loads lazy.

I set the entire entity manager to load only from source. 


Posted By: orcities
Date Posted: 10-Oct-2012 at 8:56am
Nevermind got it.


Posted By: kimj
Date Posted: 10-Oct-2012 at 9:22am
OK :).
 
The EntityReferenceLoadStrategy of "Load" will always force the navigation property to be reloaded from the data source.   And when async navigation is enabled, as it will be in Windows Store apps, the resulting navigation will be a pending entity or pending entity list.
 
 


Posted By: orcities
Date Posted: 10-Oct-2012 at 9:25am
To resolve I set load strategy on my entire EntityManager to EntityReferenceLoadStrategy.DoNotLoad.

When I run: 

 return await DataService.EntityManager.OrganizationContactAssociations
                .Where(e => e.InActive == false && e.ContactId == pContactId)
                .ExecuteAsync();

I get the appropriate records

When I run:
 return await DataService.EntityManager.OrganizationContactAssociations
                .Where(e => e.InActive == false && e.ContactId == pContactId)
                .Include(e => e.ContactEmail)
                .Include(e => e.ContactEmail.Email)
                .Include(e => e.ContactJobTitle)
                .Include(e => e.ContactJobTitle.JobTitle)
                .ExecuteAsync();

I get nothing.

If I add 1 include I get records, 2 or more and I get nothing.

It appears that the Include is performing a join.

Can you lend me some guidance.


Posted By: kimj
Date Posted: 10-Oct-2012 at 9:44am
EF generates the SQL based on the relationships in the model, and will use inner and outer joins as needed for the Included relations.
 
I'd check the generated SQL (SQL Server Profiler is a handy tool if you're running SQL Server) to see what the generated query looks like.  If there are any inner joins, then I'd double check the definition of that relationship in the EDM.


Posted By: orcities
Date Posted: 10-Oct-2012 at 10:23am
They are coming back as Inner Joins. How do i change it to be Left or Right joins? My assumptions is that an Include would only pull that data that applies, implying a left/right join, and not an inner.


Posted By: orcities
Date Posted: 10-Oct-2012 at 10:43am
The Entity I am trying to query is an association table with a type column. The type column determines one entity it links off to. Example. OrganizationContactAssociation { OrganizationId, ContactId, Id, AssociationType }
If AssociationType = Email then Id is an EmailId and the Navigation Property for ContactEmail is the correct one to use.

Because of this I tried:
return await DataService.EntityManager.OrganizationContactAssociations
                .Where(e => e.InActive == false && e.ContactId == pContactId)
                .Include(e => e.ContactAddress)
                .Include(e => e.ContactAddress.Address)
                .Include(e => e.ContactAddress.Type)
                .Include(e => e.ContactPhone)
                .Include(e => e.ContactPhone.Phone)
                .Include(e => e.ContactPhone.Type)
                .Include(e => e.ContactJobTitle)
                .Include(e => e.ContactJobTitle.JobTitle)
                .Include(e => e.ContactEmail)
                .Include(e => e.ContactEmail.Email)
                .ExecuteAsync();

This causes an inner join on each table rather then a left join.

I tried changing the Id to allow nulls which then allows the references to be 0 to many. This causes an error.

I tried creating Entities of type OrganizationContactAssociation for each type. This does not allow me to query those sub/inherited entities directly.

I tried querying each individual type separately, this causes only the last query to be in the cache. I can not allow for the lazy load type because I have a deleted state to each entity, and do not delete any entities. Therefore, I can only display those entities where the deleted states is false.





Print Page | Close Window