13

In the controllers generated by Visual Studio, as well as the sample application (ContosoUniversity), the Index action always has something like

var departments = db.Departments.Include(d => d.Administrator);

What's the difference between that and

var departments = db.Departments;

First I suspected that the first one (with Include) enables the view to retrieve department.Administrator. But the second one (without Include) seems to be able to do that as well.

Bart
  • 19,692
  • 7
  • 68
  • 77
Jim
  • 1,695
  • 2
  • 23
  • 42

4 Answers4

17

The Include tells Entity Framework work to eagerly load the Administrator for each Department in the results. In this case, Entity Framework can use a SQL join to grab the data from both tables in a single request.

The code will still work without the Include, but the first time you access a Department's Administrator, EF will need to hit the database to load it (since it wasn't pre-loaded). Loading data on demand (lazily) is a nice feature but it can be a serious performance problem (known as an N+1 problem). Especially if you are accessing the Administrator for each Department (for example, in a loop) - instead of one database call, you will end up with many!

Community
  • 1
  • 1
Stephen McDaniel
  • 2,938
  • 2
  • 24
  • 53
  • 1
    Not sure if I understood it correctly. With Include, EF will use join to grab both Departments and their Administrators in a single query. Without Include, EF will only grab Departments and then grab their Administrators on demand? So if I only need one or two Administrator in the Deparments, I shouldn't use Include. But if I need the Administrator of all (or most) Deparments, I should use Include. – Jim Sep 10 '11 at 08:44
  • 2
    Exactly, the include forces the Administrators to be loaded in the same query that loads the Departments. Without the include, the Administrators are loaded one at a time on demand. – Stephen McDaniel Sep 10 '11 at 08:45
  • @Jim, specifically, since you use ASP.NET MVC, you don't want your Views to contact the database. And for `departments.First().Administrator` to work without database access, `Administrator` needs to be pre-loaded using `Include`. – bzlm Sep 11 '11 at 16:48
  • @Jim if you have one to many relation in your models then go for lazy loading, if many to many then eager loading would be better choice. – Cavid Hummatov Mar 10 '19 at 14:57
5

In first case (with Include) when you write department.Administrator servers the object from memory that has been eagerly loaded due to Include method. In the second case, an sql statement will be executed to fetch the Administrator record from the db for each department object.

Muhammad Adeel Zahid
  • 17,474
  • 14
  • 90
  • 155
3

See the "Lazy, Eager, and Explicit Loading of Related Data" section in this tutorial: http://www.asp.net/entity-framework/tutorials/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application

tdykstra
  • 5,880
  • 2
  • 23
  • 20
2
var departments = db.Departments;

This will retrieves the aggregate domains only if LazyLoadingEnabled is enabled & MultipleActiveResultSets is set to true in connection string.

Charandeep Singh
  • 942
  • 2
  • 7
  • 17