Wednesday, March 5, 2014

Entity Framework: Eager Load Data When In Projections

Entity Framework supports eager loading and data projections.
Eager Loading: loading related entities together to avoid multiple database calls
Projections: Retrieve a set of data entities from database and return a converted set of data entities

Till today, I thought EF eager loading and projections are working together in a single query.

Problem

I had a query similar to below:





When I opened Entity Framework Profiler, it always displayed multiple additional queries to load 'EmailAddresses'. So, eager loading was not working at all.

Then I did bit of googling and found that EF eager loading and projections are not working together.

Solution

I did following workaround to load data together.

First created a wrapper property to the property I needed to eager load:




Updated the data query to set the wrapper property:


It worked without any issue.


No comments: