Explicit Loading in Entity Framework

Explicit Loading in Entity Framework

In this article, I am going to discuss Explicit Loading in Entity Framework with Examples. Please read our previous article where we discussed Lazy Loading in Entity Framework. At the end of this article, you will understand what is Explicit Loading and how to implement Explicit loading in the Entity Framework. Explicit loading is valid in EF 6 and EF Core both.

Note: We are going to work with the same example that we created in our Introduction to Entity Framework Database First Approach article. Please read our introduction to Entity Framework Database First article before proceeding to this article.

Explicit Loading in Entity Framework

Even with lazy loading disabled (in EF 6), it is still possible to lazily load related entities, but it must be done with an explicit call. Use the Load() method to load related entities explicitly. First disabled Lazy loading for all the entities by setting LazyLoadingEnabled flag on the Configuration property to false as shown below.

Explicit Loading in Entity Framework

Then Modify the Main method of the Program class as shown below.

using System;
using System.Linq;
namespace DBFirstApproach
{
    class Program
    {
        static void Main(string[] args)
        {
            using (EF_Demo_DBEntities context = new EF_Demo_DBEntities())
            {
                var student = context.Students
                             .FirstOrDefault(s => s.StudentId == 1);

                // loads StudentAddress
                context.Entry(student).Reference(s => s.StudentAddress).Load();

                // loads Courses collection
                context.Entry(student).Collection(s => s.Courses).Load(); 

                Console.WriteLine($"Firstname: {student.FirstName}, Lastname: {student.LastName}, Address: {student.StudentAddress.Address1}");
                foreach (var course in student.Courses)
                {
                    Console.WriteLine($"CourseName: {course.CourseName}");
                }

                Console.Read();
            }
        }
    }
}

In the above example, context.Entry(student).Reference(s => s.StudentAddress).Load() loads related the StudentAddress entity. The Reference() method is used to get an object of the specified reference navigation property and the Load() method loads it explicitly.

In the same way, context.Entry(student).Collection(s => s.Courses).Load() loads the collection navigation property Courses of the Student entity. The Collection() method gets an object that represents the collection navigation property.

The Load() method executes the SQL query in the database to get the data and fill up the specified reference or collection property in the memory.

In the next article, I am going to discuss working with Entity Framework with Disconnected Entities. In this article, I try to explain Explicit Loading in Entity Framework and I hope you enjoyed this Explicit Loading in Entity Framework article. Please give your valuable feedback and suggestions about this article.

Leave a Reply

Your email address will not be published. Required fields are marked *