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 use of the Include method
using (var context = new UnicornsContext())
{
// Load all princesses and related unicorns
var princesses1 = context.Princesses
.Include(p => p.Unicorns)
.ToList();
// Load one princess and her related unicorns
var princess1 = context.Princesses
.Where(p => p.Name == "Cinderella")
.Include(p => p.Unicorns)
.FirstOrDefault();
// Load all princesses and related unicorns using a string
// to specify the relationship
var princesses2 = context.Princesses
.Include("Unicorns")
.ToList();
// Load one princess and her related unicorns using a string
// to specify the relationship
var princess2 = context.Princesses
.Where(p => p.Name == "Cinderella")
.Include("Unicorns")
.FirstOrDefault();
}
Note that Include is an extension method in the System.Data.Entity namespace