I have two objects that have a many to many relationship with each other.
I'm using Entity Framework in a Database First approach.
My Database Profiler is showing that everytime I fetch one set of objects A, it loads the other set of object B for each element in A. I assumed that with lazy loading, this wouldn't happen, or that B would be fetched when accessing via the navigation property.
The code to access the objects uses a generic approach, where entities is my DbContext
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = entities.Set<T>();
return query;
}
Both navigation properties are implemented as virtual ICollection<T>
and I have Configuration.LazyLoadingEnabled = true
explicitly set in my DbContext
constructor.
Am I missing something, or approaching this the wrong way? Should I just remove the navigation properties and explicitly load what I need via other queries?
A
elements in the debugger? Remember that you trigger lazy loading with the debugger when you watch theA
elements with theirB
collections. – SlaumaLazyLoadingEnabled
to false and then theB
collections aren't loaded, so I'm not sure why that is – Andrew BurgessToList()
on theIQueryable
cause the lazy loading to be executed? Without using the debugger, and just using the SQL Server Profiler, I can see that eachA
object is having theB
objects selected, and I don't actually use the navigation property anywhere – Andrew BurgessInclude
in your query? – Slauma