- Entity Framework Tutorial
- Entity Framework - Home
- Entity Framework - Overview
- Entity Framework - Architecture
- Entity F - Environment Setup
- Entity Framework - Database Setup
- Entity Framework - Data Model
- Entity Framework - DbContext
- Entity Framework - Types
- Entity Framework - Relationships
- Entity Framework - Lifecycle
- Entity F - Code First Approach
- Entity F - Model First Approach
- Entity F - Database First Approach
- Entity Framework - DEV Approaches
- Entity F - Database Operations
- Entity Framework - Concurrency
- Entity Framework - Transaction
- Entity Framework - Views
- Entity Framework - Index
- Entity F - Stored Procedures
- Entity F - Disconnected Entities
- Entity F - Table-Valued Function
- Entity Framework - Native SQL
- Entity Framework - Enum Support
- Entity F - Asynchronous Query
- Entity Framework - Persistence
- Entity F - Projection Queries
- Entity F - Command Logging
- Entity F - Command Interception
- Entity Framework - Spatial Data Type
- Entity Framework - Inheritance
- Entity Framework - Migration
- Entity Framework - Eager Loading
- Entity Framework - Lazy Loading
- Entity Framework - Explicit Loading
- Entity Framework - Validation
- Entity Framework - Track Changes
- Entity Framework - Colored Entities
- Entity F - Code First Approach
- Entity Framework - First Example
- Entity Framework - Data Annotations
- Entity Framework - Fluent API
- Entity Framework - Seed Database
- Entity F - Code First Migration
- Entity F - Multiple DbContext
- Entity F - Nested Entity Types
- Entity Framework Resources
- Entity Framework - Quick Guide
- Entity Framework - Useful Resources
- Entity Framework - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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: Ali 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 == "Ali") .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.