Last active
August 29, 2015 14:16
-
-
Save kkoziarski/584bd7fb34a3a99ddb61 to your computer and use it in GitHub Desktop.
Lazy, Eager, and Explicit Loading of Related Data http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Eager loading. When the entity is read, related data is retrieved along with it. | |
//This typically results in a single join query that retrieves all of the data that's needed. | |
//You specify eager loading by using the Include method. | |
//Query: All department rows and all related rows in one query | |
var departments = db.Departments.Include(d => d.Courses); | |
foreach (var department in departments) | |
{ | |
foreach (var course in department.Courses) | |
{ | |
courseList.Add(department.Name + course.Title); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Explicit loading. This is similar to lazy loading, except that you explicitly | |
//retrieve the related data in code; it doesn't happen automatically | |
//when you access a navigation property. You load related data manually by getting the object state | |
//manager entry for an entity and calling the Collection.Load method for collections | |
//or the Reference.Load method for properties that hold a single entity. | |
//(In the following example, if you wanted to load the Administrator navigation property, | |
//you'd replace Collection(x => x.Courses) with Reference(x => x.Administrator).) | |
//Typically you'd use explicit loading only when you've turned lazy loading off. | |
var departments = db.Departments.Include(d => d.Courses); | |
foreach (var department in departments) //Query: all Department rows | |
{ | |
db.Entry(department).Collection(x => x.Courses).Load(); //Query: Course rows related to Department 'department' | |
foreach (var course in department.Courses) | |
{ | |
courseList.Add(department.Name + course.Title); | |
} | |
} | |
// Lazy loading | |
//viewModel.Enrollments = viewModel.Courses.Where( | |
// x => x.CourseID == courseID).Single().Enrollments; | |
// Explicit loading | |
var selectedCourse = viewModel.Courses.Where(x => x.CourseID == courseID).Single(); | |
db.Entry(selectedCourse).Collection(x => x.Enrollments).Load(); | |
foreach (Enrollment enrollment in selectedCourse.Enrollments) | |
{ | |
db.Entry(enrollment).Reference(x => x.Student).Load(); | |
//Notice that you use the Collection method to load a collection property, but for a property that holds just one entity, you use the Reference method. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Lazy loading. When the entity is first read, related data isn't retrieved. | |
//However, the first time you attempt to access a navigation property, | |
//the data required for that navigation property is automatically retrieved. | |
//This results in multiple queries sent to the database — one for the entity itself | |
//and one each time that related data for the entity must be retrieved. | |
//The DbContext class enables lazy loading by default. | |
var departments = db.Departments; | |
foreach (var department in departments) //Query: all Department rows | |
{ | |
foreach (var course in department.Courses) //Query: Course rows related to Department 'department' | |
{ | |
courseList.Add(department.Name + course.Title); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment