In this Entity Framework Tutorial, we are going to understand about an important concept related to Object Relational Mapping that is “Eager Loading” with the help of a practical example. We already have discussed eager loading at high level, and another related concept “Lazy Loading” that is opposite to Eager Loading while discussing Entity Framework Interview Questions along with more Entity Framework concepts. To see a practical example for Lazy Loading in Entity Framework, follow here.
Eager loading is the process by which a query loads related entities along with it as part of the query. It is achieved by the using the “Include” method. For example, when querying orders, eager-load their product details also in a single query.
In order to practically grasp Entity Framework, we highly recommend to go through these real time implementations:
- Building Your First MVC Application with Entity Framework [Part 1 & Part 2]
- Understanding Model First Approach in MVC5 with EF6
- What’s new in Entity Framework from Beginning to EF6
- Developer Jobs (ASP.NET MVC | Entity Framework)
We have created 3 entities, Category, Products and Orders. In below diagram, entities with their relationships look as:Image may be NSFW.
Clik here to view.
The class of Products will look as:
public partial class Product { public Product() { this.Orders = new HashSet<Orders>(); } public int ProductId { get; set; } public string ProductName { get; set; } public string Price { get; set; } public int CategoryId { get; set; } public virtual Category Category { get; set; } public virtual ICollection<Orders> Orders { get; set; } }
The class of order entity will look as:
public partial class Order { public int OrderId { get; set; } public string ProductCount { get; set; } public int ProductName { get; set; } public virtual Product Product { get; set; } }
Following code does the eager loading in case of products and orders:
public ActionResult Index() { var orders = db.Orders.Include(o => o.Product); return View(orders.ToList()); } <table class="table"> <tr> <th> Product Name </th> <th> Product Count </th> <th> Total </th> <th> </th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Product.ProductName) </td> <td> @Html.DisplayFor(modelItem => item.ProductCount) </td> <td> @(Convert.ToDouble(item.Product.Price) * Convert.ToDouble(item.ProductCount)) </td> <td> @Html.ActionLink("Edit", "Edit", new { id = item.OrderId }) | @Html.ActionLink("Details", "Details", new { id = item.OrderId }) | @Html.ActionLink("Delete", "Delete", new { id = item.OrderId }) </td> </tr> } </table>
Resultant screen will be:Image may be NSFW.
Clik here to view.
In order to create an order, we can do as:
public ActionResult Create() { ViewBag.ProductName = new SelectList(db.Products, "ProductId", "ProductName"); return View(); } div class="form-group"> @Html.LabelFor(model => model.ProductName, "ProductName", htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.DropDownList("ProductName", null, htmlAttributes: new { @class = "form-control" }) @Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.ProductCount, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.ProductCount, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.ProductCount, "", new { @class = "text-danger" }) </div> </div>
- Learn ASP NET MVC 5 step by step [Maruti Makwana, Corporate Trainer]
28 Lectures, 2.5 Hours Video, Intermediate Level
Very easy to learn video series on Asp.Net MVC 5 Specially for those who are familiar with Asp.Net Web forms. - AngularJS for ASP.NET MVC Developers [Brett Romero]
10 Lectures, 1 hour video, Intermediate Level
The Fastest Way For .NET Developers To Add AngularJS To Their Resume - ASP.NET with Entity Framework from Scratch [Manzoor Ahmad, MCPD | MCT]
77 Lectures, 10 hour video, All Level
Latest approach of web application development - Comprehensive ASP.NET MVC [3D BUZZ]
34 lectures, 14 Hours Video, All Levels
From zero knowledge of ASP.NET to deploying a complete project to production.
Clik here to view.

This screen will place an order of products. As we have added foreign key constraint in orders with products. So with eager loading we can load the related product details while loading orders.
var orders = db.Orders.Include(o => o.Product);
Image may be NSFW.
Clik here to view.
After reading this Entity Framework Tutorial, it will be easy to implement eager loading in our application. Follow here to understand that correctly choosing between eager-loading and lazy-loading in practical scenarios.
- A. EDM Generator (EdmGen.exe)
- B. ADO.NET Entity Data Model Designer
- C. Update Model Wizard
- D. Entity Data Model Wizard
- E. All of Above
- F. None of Above
For a complete online test and Practice Exams on Web Technologies, Click Here.
Correct Answer: B, D
Top Technical Interview Questions and Answers Series:
- Top 10 ASP.NET MVC Interview Questions
- Top 10 ASP.NET Web API Interview Questions
- Top 10 HTML5 Interview Questions
- Top 10 ASP.NET Interview Questions
- Comprehensive Series of ASP.NET Interview Questions
- ReactJs Interview Questions and Answers
- Top 10 ASP.NET Web API Interview Questions
- Top 10 ASP.NET AJAX Interview Questions
- Top 10 WCF Interview Questions
- Comprehensive Series of WCF Interview Questions
- Top 20 AngularJS Interview Questions
- MUST Have AngularJS 2 Interview Questions
ASP.NET MVC Jobs [Updated Daily]
Top ASP.NET MVC Jobs
Senior Software Engineer
|
Seattle, WA 98104 | 29-December-2016 |
ASP.NET/ANGULAR JS DEVELOPER
|
Dallas, TX | 16-December-2016 |
ASP.NET/Angular Developer
|
United States | 16-December-2016 |
Senior Software Engineer
|
Seattle, WA 98102 | 09-December-2016 |
SDE3(Full stack Developer)
|
Redmond, WA | 29-December-2016 |
.NET Senior Software Engineer
|
Atlanta, GA | 28-December-2016 |
.Net Developer Senior
|
Roseland, NJ | 21-December-2016 |
Senior Software Engineer
|
Seattle, WA | 06-December-2016 |
Senior .Net Developer
|
Dallas, TX | 19-December-2016 |
Senior .Net Developer - Locals only
|
Atlanta, GA | 19-December-2016 |
Backend Developer II
|
Seattle, WA | 28-December-2016 |
C# Software Developer (AngularJS)
|
Redmond, WA | 23-December-2016 |
Senior .Net Developer
|
New York, NY | 16-December-2016 |
Senior . Net Web Developer with Angular JS
|
Harrisburg, PA | 13-December-2016 |
Senior .Net Front End Web Developer
|
Houston, TX | 12-December-2016 |
.Net Developer
|
Akron, OH | 15-November-2016 |
Senior Software Engineer
|
Canton, OH | 13-December-2016 |
Web developer(Internal)
|
Redmond, WA | 21-December-2016 |
Senior .NET Developer
|
Gilbert, AZ | 06-December-2016 |
Senior Software Engineer
|
Cleveland, OH 44135 | 12-December-2016 |
Jr .Net Developer
|
Redmond, WA | 29-December-2016 |
Senior Software Engineer, Ed-Fi Alliance - Austin, TX
|
Austin, TX 78746 | 08-December-2016 |
Senior Software Engineer
|
Chicago, IL 60606 | 06-December-2016 |
Senior Software Developer - .NET/C#
|
Alpharetta, GA 30009 | 29-November-2016 |
.Net Developer with Visual Studio 2015, TFS,
|
Columbia, SC | 29-December-2016 |
The post Entity Framework 6 Eager Loading appeared first on Web Development Tutorial.