There are three ways in which you can load related entities using Entity Framework. These are—eager loading, lazy loading, and explicit loading. In eager loading, a query on a particular type of an entity loads the related entities also as part of the query execution. The following code snippet illustrates how you can implement eager loading:
using (SecurityDBEntities context = new SecurityDBEntities()) { // Load all controls and control types var data = context.Controls .Include(b => b.ControlTypes) .ToList(); }
You can load entities lazily even if lazy loading is disabled by making an explicit call. This is known as explicit loading and is illustrated in the following code snippet:
using (SecurityDBEntities context = new SecurityDBEntities()) { var data = context.Controls.Find(2); }
Lazy loading is a feature that enables you to load data only at the time when it is requested...