2
votes

I have read about lazy loading from this web site. Enable or disable LazyLoading

"If we request a list of Students with LazyLoading enabled, the data provider will get all of our students from the DB but each StudentAddress property won’t be loaded until the property will be explicitly accessed."

This statement says that when I set Lazy Loading Enabled = true the related data won't be loaded. However

List<Students> stdList = Datacontext.Students.ToList();

if I set lazy loading enabled = true the above code returns all stundents with their teachers and address. What is the point that I am missing here? Please could someone explain it?

2
LazyLoading is simply loading the data when it is referred to (in linq terms) instead of pre-loading it onto the object whether or not it's output. Your example above would lazily load Addresses if you said: foreach(var student in stdList){ @stdList.Address.City}. Generally, stay away from LazyLoading as it could cause the n+1 scenario of bombarding your db server with requests. - BlackjacketMack

2 Answers

1
votes

No matter what setting you have, if you use .ToList() it will "enumerate the enumerable". This is very significant, and this phrase should become common knowledge to you.

When .ToList() is used, many things occur. Enumerating the enumerable means that the previous set of how to enumerate the set is now being used to actually iterate through the set and populate the data. What that means is that the previous enumerator (which was stored internally as an Expression Tree) is now going to be sent from Entity Framework to your SQLProvider Factory. That will then convert the object graph from the Expression Tree into SQL and execute the query on the server, thus returning the data and populating your list.

Lazy loading instead of using ToList() would be if you had this IQueryable enumerable, and then iterated that manually loading each element in the set, or only partial elements in the set.

Once you have the list of elements returned, lazy loading will only come in to play if there are navigational properties. If there were related properties, for example if you have an Invoice and you want to get the related Customer information from the customer table. The relation will not explicitly be returned at first, only the invoices. So to get the customer data you could then (while the context was still open, i.e. not disposed) access that via the .Customer reference on your object and it would load. Conversely, to load all the customers during the original enumeration, you could use the .Include() functionality on your queryable, and that would then tell the sql provider factory to use a join when issuing the query.


In your specific example,

List<Students> stdList = Datacontext.Students.ToList();

This will actually not load all of the Teachers and Addresses regardless of if lazy loading is enabled or not. It will only load the students. If you want to lazy load a Teacher, while the Datacontext is still not disposed, you could then use

var firstStudent = stdList.First();
var teacher = firstStudent.Teacher; 
//and at this point lazy loading will fetch the teacher 
//by issuing **another** query (round trip) to the database

That would only be possible if lazy loading were enabled.


The alternative to this is to eager load, which would include the teachers and addresses. That would look like this

List<Students> stdList = Datacontext.Students
    .Include( s => s.Teacher )
    .Include( s => s.Address ).ToList();

And then later on if you were to try to access a teacher the context could be disposed and access would still be possible because the data was already loaded.

var firstStudent = stdList.First();
var teacher = firstStudent.Teacher; 
//and at this point the teacher was already 
//loaded and as a result no additional round trip is required
1
votes

How was that you noticed that property was loaded, using the debugger? If that is the case, then you already have the answer. With the debugger you are accessing to that property too, so, that also triggers lazy loading.

How this works?

If your entities meets these requirements, then EF will create a proxy class for each of your entities that support change tracking or lazy loading. This way you can load the related entities only when these are accessed. As I explained earlier, the debugger will also trigger lazy loading.

Now, be careful with lazy loading, once you context has been disposed, you will get an exception when you try to get access to one of the related properties. So, I would suggest to use eager loading in that case.