- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Entity Framework - Eager Loading
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager loading is achieved by the use of the Include method.
It means that requesting related data be returned along with query results from the database. There is only one connection made to the data source, a larger amount of data is returned in the initial query.
For example, when querying students, eager-load their enrollments. The students and their enrollments will be retrieved in a single query.
Let’s take a look at the following example in which all the students with their respective enrollments are retrieved from the database by using eager loading.
class Program { static void Main(string[] args) { using (var context = new UniContextEntities()) { // Load all students and related enrollments var students = context.Students .Include(s ⇒ s.Enrollments).ToList(); foreach (var student in students) { string name = student.FirstMidName + " " + student.LastName; Console.WriteLine("ID: {0}, Name: {1}", student.ID, name); foreach (var enrollment in student.Enrollments) { Console.WriteLine("Enrollment ID: {0}, Course ID: {1}", enrollment.EnrollmentID, enrollment.CourseID); } } Console.ReadKey(); } } }
When the above code is compiled and executed you will receive the following output.
ID: 1, Name: Ap Alexander Enrollment ID: 1, Course ID: 1050 Enrollment ID: 2, Course ID: 4022 Enrollment ID: 3, Course ID: 4041 ID: 2, Name: Meredith Alonso Enrollment ID: 4, Course ID: 1045 Enrollment ID: 5, Course ID: 3141 Enrollment ID: 6, Course ID: 2021 ID: 3, Name: Arturo Anand Enrollment ID: 7, Course ID: 1050 ID: 4, Name: Gytis Barzdukas Enrollment ID: 8, Course ID: 1050 Enrollment ID: 9, Course ID: 4022
Below are some of the other forms of eager loading queries which can be used.
// Load one Student and its related enrollments var student1 = context.Students .Where(s ⇒ s.FirstMidName == "Ap") .Include(s ⇒ s.Enrollments).FirstOrDefault(); // Load all Students and related enrollments // using a string to specify the relationship var studentList = context.Students .Include("Enrollments").ToList(); // Load one Student and its related enrollments // using a string to specify the relationship var student = context.Students .Where(s ⇒ s.FirstMidName == "Salman") .Include("Enrollments").FirstOrDefault();
Multiple Levels
It is also possible to eagerly load multiple levels of related entities. The following queries show examples of Student, Enrollments and Course.
// Load all Students, all related enrollments, and all related courses var studentList = context.Students .Include(s ⇒ s.Enrollments.Select(c ⇒ c.Course)).ToList(); // Load all Students, all related enrollments, and all related courses // using a string to specify the relationships var students = context.Students .Include("Enrollments.Course").ToList();
We recommend that you execute the above example in a step-by-step manner for better understanding.
Advertisements