Skip to content

EF Lazy Loading

Omar Piani edited this page Mar 6, 2018 · 2 revisions

Entity Framework and Entity Framework Core uses Include methods to manage Lazy Loading

Get, GetAll, Find and FindAll methods have support for Include commands.

They can be passed in a several ways directly in methods, as lambda function

repository.GetAll(x => x.EmailAddresses);

In many cases you may need pass multiple includes, or navigate child members.

repository.Get(id, includePaths: new Expression<Func<Contract, object>>[] { c => c.Employee, c => c.Owners.First().Address });

or with string name, good for dynamic query

repository.GetAll("Employee", "Owners.Address" });

or with strategies

var strategy = new GenericFetchStrategy<Contract>();
strategy.Include(x => x.Employee);
strategy.Include(x => x.Owners.First().Address);
repository.Get(id, strategy);

or using specification

var spec = new Specification<Contract>(obj => obj.Employee.Name == "Jeff");
spec.FetchStrategy = new GenericFetchStrategy<Contract>();
spec.FetchStrategy
                .Include(x => x.Employee)
                .Include(x => x.Owners.First().Address);
repository.FindAll(spec);

Clone this wiki locally