Lazy Loading in Entity Framework
ets write a simple query to fetch the top 20 employees from the Northwind database and count the number of orders placed by each employee. NORTHWNDEntities context = new NORTHWNDEntities(); var employees = context.Employees.Take(20).ToList(); foreach (Employee employee in employees) { Console.WriteLine("Total number of order by employee : {0}", employee.Orders.Count); } We start off with creating the ObjectContext which is NORTHWNDEntities in our case. We filter the 20 employees using Take method on the employees entity collection. Then we loop over the individual employees and display the count of Orders to the console window. By default Entity Framework 4 enables the Lazy Loading feature. Because of this there are multiple hits to the database server. The initial query which filters the 20 employees is executed when we start iterating over the loop. And then for each employee a separate query i...


Comments