Entity Framework Eager Loading and Lazy Loading Produce Same Queries<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Entity Framework Eager Loading and Lazy Loading Produce Same Queries

Posted on: June 19, 2014

If you are using Entity Framework with Lazy Loading or Eager Loading you may realize that both work the same way. With Lazy Loading, you have to access the property and Entity Framework load the property on-demand. On the other side, with Eager Loading, we need to specify the property to load before accessing it. Lazy Loading has the disadvantage that you must have virtual property and has some overhead code that is executed to remember if it must load from the database or from the context, while Eager Loading lets you handle everything so you can optimized your database calls.

In a scenario where you have a one to one relationship, for example a Person that has an House.

1var person = new Person {Id = 1, Name = "SeededPerson", BirthDate = new DateTime(1900, 1, 1), Friends = new Collection<Person> {person2, person3}}; person.Residence = new House {Id = 1, Address = new Address{City="Montreal", Number = 123, Street = "Owl"}, Price = 350000}; context.Persons.Add(person); [/cshapr]
3You can load the Residence by using Eager Loading this way:
7 private static void LazyLoadingAndEagerLoadingSameResult() { using (var context = new YourContext()) { Console.WriteLine("No Lazy Loading, Eager Loading"); var person = context.Persons.Find(1); context.Entry(person).Reference(d=>d.Residence).Load(); Console.WriteLine("City is " + person.Residence.Address.City); } }

If we kick in the Microsoft SQL Profiler, we see two queries. One that load the Person and one that load the Residence object.

1SELECT [Limit1].[Id] AS [Id], [Limit1].[Name] AS [Name], [Limit1].[BirthDate] AS [BirthDate], [Limit1].[Residence_Id] AS [Residence_Id], [Limit1].[House_Id] AS [House_Id] FROM ( SELECT TOP (2) [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent1].[BirthDate] AS [BirthDate], [Extent1].[Residence_Id] AS [Residence_Id], [Extent1].[House_Id] AS [House_Id] FROM [dbo].[People] AS [Extent1] WHERE [Extent1].[Id] = @p0 ) AS [Limit1]
3SELECT [Extent2].[Id] AS [Id], [Extent2].[Price] AS [Price], [Extent2].[Address_Street] AS [Address_Street], [Extent2].[Address_Number] AS [Address_Number], [Extent2].[Address_City] AS [Address_City], [Extent2].[Owner_Id] AS [Owner_Id] FROM [dbo].[People] AS [Extent1] INNER JOIN [dbo].[Houses] AS [Extent2] ON [Extent1].[Residence_Id] = [Extent2].[Id] WHERE ([Extent1].[Residence_Id] IS NOT NULL) AND ([Extent1].[Id] = @EntityKeyValue1)

The Lazy Loading version is similar but without the Reference/Load line.

1using (var context = new YourContext()) { context.Configuration.LazyLoadingEnabled = true; Console.WriteLine("Lazy Loading, No Eager Loading"); var person = context.Persons.Find(1); Console.WriteLine("City is " + person.Residence.Address.City); }

It also require you to change the Person class to have the Residence to be virtual.

1public class Person { public int Id { get; set; } public string Name { get; set; } public DateTime BirthDate { get; set; }
3public ICollection<Person> Friends { get; set; }
5public virtual House Residence { set; get; } }

If you forget to set the virtual, you will not receive an error message about the virtual but about a NullReferenceException. Unfortunately, the exception does not speak for itself! You have to remember to change the property to virtual.

You can find the source code of this article on GitHub or inside this Zip File.