Back to: Entity Framework Tutorials For Begineers and Professionals
Explicit Loading in Entity Framework with Examples
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 vs Eager Loading in Entity Framework with Examples. 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 was introduced with Entity Framework 6. 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 if Lazy Loading is disabled in Entity Framework 6, it is still possible to lazily load the related entities, but it must be done with an explicit call. Here, we need to call the Load() method to load the related entities explicitly.
Let us understand this with an example. First disabled the Lazy loading for all the entities by setting the LazyLoadingEnabled flag on the Configuration property to false as shown below in the context class of our application.
Then Modify the Main method of the Program class as shown below to use explicit loading in Entity Framework. The following example code is self-explained, so please go through the comment lines.
using System; using System.Linq; namespace DBFirstApproach { class Program { static void Main(string[] args) { using (EF_Demo_DBEntities context = new EF_Demo_DBEntities()) { //To check what database script is generated and executed on the database server by Entity Framework context.Database.Log = Console.Write; //Load the Student Entity, as Lazy Loading is Disabled, it will only load the Student Entity var student = context.Students.FirstOrDefault(s => s.StudentId == 1); //Load StudentAddress Explicit using the Load Method //If the related Property is a Reference type, //First, call the Reference method by passing the property name that you want to load //and then call the Load Method context.Entry(student).Reference(s => s.StudentAddress).Load(); //Loads Courses Collection Explicit using the Load Method //If the related Property is a Collection type, //First, call the Collection method by passing the property name that you want to load //and then call the Load Method context.Entry(student).Collection(s => s.Courses).Load(); //Now, the student entity related properties are lazily loaded, so you can access them 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(); } } } }
Now, run the above code and you will get the following output. As you can see in the below output, it is using Explicit Loading to load the related entities. Here, first, it issues one select statement to fetch the student entity and then it issues two separate SQL queries to get the related student address and course data.Â
In the above example, we need to understand two statements i.e. context.Entry(student).Reference(s => s.StudentAddress).Load() and context.Entry(student).Collection(s => s.Courses).Load(). So, let us proceed and understand these two statements in detail.
- context.Entry(student).Reference(s => s.StudentAddress).Load(): If you want to load the related entity data which is a reference navigation property of the Main entity, then you need to use this syntax. This statement will generate and execute the SQL query in the database to fetch the related StudentAddress data and fill the data with the StudentAddress reference property of the student object.
- context.Entry(student).Collection(s => s.Courses).Load(): If you want to load the related entity data which is a collection navigation property of the Main entity, then you need to use this syntax. This statement will generate and execute the SQL query in the database to fetch the related Courses data and fill the data with the Courses Collection property of the student object.
Here, to the Entry method you need to pass the main entity instance. In this case, the student instance or object is our main entity. Then you need to check whether the related entity is a reference-type navigation property or a collection-type navigation property. If it is a reference navigation property, then you need to call the Reference method and you need to specify the Reference Navigation Property name using the string name or by using the lambda expression. And if it is a collection navigation property, then you need to use the Collection method and you need to specify the Collection Navigation Property name using the string name or by using the lambda expression. And finally, you need to call the Load method, which generates and executes the SQL query in the database to get the data and fill the specified reference or collection navigation property of the main entity.
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 with Examples and I hope you enjoyed this Explicit Loading in Entity Framework with Examples article. Please give your valuable feedback and suggestions about this article.