-1

I'm building a basic MVC Core Web API and trying to connect using Entity Framework, I came across something called "Lazy Loading" and I can't seem to wrap my head around what Lazy Loading is revolving EntityFramework?

When is Lazy Loading used and how does it benefit?

ARLCode
  • 651
  • 6
  • 20
  • 1
    Possible duplicate of [Lazy Loading vs Eager Loading](http://stackoverflow.com/questions/31366236/lazy-loading-vs-eager-loading) – jegtugado Nov 09 '16 at 03:51

2 Answers2

3

In my opinion, Lazy loading is the process when an entity or collection of entities is automatically loaded from the database.

Actually, the sorting and retrieval algorithms are being applied on the user side, while query gets all the data. By the default the Lazy Loading feature is ON, but can be turned off manually, or you can construct clever queries, i.e. using IQueryable, which does the filtering on Server side and retrieves only those records, which are required. In practices, it will help you to save a lot of time and resources, by preventing the heavy traffic and overloading server

Better answer is on this website-> https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx

  • Thank you! That's all I was looking for a simple break down of how it works, this is new territory for me so I just want to make sure I'm doing it right. – ARLCode Nov 09 '16 at 05:53
2

From entityframeworktutorial site:

Lazy loading means delaying the loading of related data, until you specifically request for it. For example, Student class contains StudentAddress as a complex property. So, the context first loads all the students from the database, then it will load the address of a particular student when we access StudentAddress property as shown below.

using (var ctx = new SchoolDBEntities())
{
    //Loading students only
    IList<Student> studList = ctx.Students.ToList<Student>();

    Student std = studList[0];

    //Loads Student address for particular Student only (seperate SQL query)
    StudentAddress address = std.StudentAddress;
}

The code shown above will result in two SQL queries. First, it will fetch all students:

SELECT 
[Extent1].[StudentID] AS [StudentID], 
[Extent1].[StudentName] AS [StudentName], 
[Extent1].[StandardId] AS [StandardId]
FROM [dbo].[Student] AS [Extent1]

Then when control reach to following line:

StudentAddress address = std.StudentAddress;

EF will send the second query to db to load StudentAddress:

exec sp_executesql N'SELECT 
[Extent1].[StudentID] AS [StudentID], 
[Extent1].[Address1] AS [Address1], 
[Extent1].[Address2] AS [Address2], 
[Extent1].[City] AS [City], 
[Extent1].[State] AS [State]
FROM [dbo].[StudentAddress] AS [Extent1]
WHERE [Extent1].[StudentID] = @EntityKeyValue1',N'@EntityKeyValue1 int',@EntityKeyValue1=1

Rules for lazy loading:

  • context.Configuration.ProxyCreationEnabled should be true.
  • context.Configuration.LazyLoadingEnabled should be true. Navigation
  • property should be defined as public, virtual. Context will NOT do lazy loading if the property is not defined as virtual(in this case the StudentAddress should be virtual).
Masoud
  • 8,020
  • 12
  • 62
  • 123