I'm eager-loading (by using .Include()) related entities in order to minimize the number of SQL queries generated for a particularly "massive" datagrid. In the POCOs, the related entities are public virtual navigation properties, meaning that they are otherwise lazy-loaded. I want to keep the default behavior of lazy-loading these related entities, as it suits me best for other parts of the project.
Now, what happens is that after eager-loading the related entities, I can see that there are still a fair amount of queries generated. And this is queries solely for related entities being null (null simply because the sql joins "found" no related data).
This led me to believe that the EF tries to lazy-load those entities (thinking they are not yet loaded) as soon as I access them later on in the presentation layer.
Have I missed something fundamental, or is there any way to work around this problem?
Sample code
Using a repository pattern, here are some simplified methods (omitting paging, sorting, filtering etc).
Get-method in generic repository
public IEnumerable<T> Get(
Expression<Func<T, object>>[] includes = null)
{
IQueryable<T> query = set;
...
// Include properies for eager loading
if (includes != null)
{
query = includes.Aggregate(query,
(current, include) => current.Include(include));
}
...
return query.ToList();
}
Above is called from a service class, something like this
...
context.Licenses.Get(
includes: new Expression<Func<License, object>>[] { l => l.Comment }
);
...
License POCO
public class License
{
public License()
{
}
// Primitive properties
public string ID { get; set; }
public string Name { get; set; }
...
// Navigation properties
public virtual LicenseComment Comment { get; set; }
...
}
LicenseComment POCO
public class LicenseComment
{
public LicenseComment()
{
}
// Primitive properties
public string LicenseID { get; set; }
public string Comment { get; set; }
}
Accessing the property in a MVC Razor view (or in a Model for that sake)
<span>@license.Comment</span>
Extra SQL queries are generated whenever I try access a License.Comment that is null (looked it up via SQL Server Profiler), which seems to me that Entity Framework lazy-loading kicks in, even though I eagerly included this property.