/// <summary> /// Demostrates how to use lazy loading for related entities /// </summary> static void LazyLoadingTest() { Console.WriteLine("---Lazy Loading---"); using (LazyLoadingEntities context = new LazyLoadingEntities()) { // Query the department entities which Budget is not NULL var departments = from d in context.Departments where d.Budget != null select d; foreach (var department in departments) { Console.WriteLine("Department: {0}", department.Name); Console.WriteLine("Courses:"); // With Lazy Loading enabled, directly access the // Courses property of the Department will load the // related course entities automatically // Note: here for each department, there will be a // seperate database call to load the course entities foreach (var course in department.Courses) { Console.WriteLine(course.Title); } Console.WriteLine(); } } }
/// <summary> /// Demostrates how to use explicit loading for related entities /// </summary> static void ExplicitLoadingTest() { Console.WriteLine("---Explicit Loading---"); using (LazyLoadingEntities context = new LazyLoadingEntities()) { // Lazy loading is enabled dy default in EF4, so turn it off // to use explicit loading later context.ContextOptions.LazyLoadingEnabled = false; // Query the department entities which Budget is not NULL var departments = from d in context.Departments where d.Budget != null select d; foreach (var department in departments) { Console.WriteLine("Department: {0}", department.Name); Console.WriteLine("Courses:"); // Explicit load the related courses entities if they // are not loaded yet // Note: here for each department, there will be a // seperate database call to load the course entities if (!department.Courses.IsLoaded) { department.Courses.Load(); } foreach (var course in department.Courses) { Console.WriteLine(course.Title); } Console.WriteLine(); } } }
/// <summary> /// Demostrates how to use eager loading for related entities /// </summary> static void EagerLoadingTest() { Console.WriteLine("---Eager Loading---"); using (LazyLoadingEntities context = new LazyLoadingEntities()) { // Lazy loading is enabled dy default in EF4, so turn it off // to use eager loading later context.ContextOptions.LazyLoadingEnabled = false; // Query the department entities which Budget is not NULL // Here we use the .Include() method to eager load the related // course entities var departments = from d in context.Departments.Include("Courses") where d.Budget != null select d; foreach (var department in departments) { Console.WriteLine("Department: {0}", department.Name); Console.WriteLine("Courses:"); // The related course entities has been already loaded // Note: for eager loading, there will be only one // database call to load the department and corresponding // course entities foreach (var course in department.Courses) { Console.WriteLine(course.Title); } Console.WriteLine(); } } }