Quantcast
Viewing latest article 6
Browse Latest Browse All 15

Entity Framework 6 Eager Loading

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:

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.
Entity Framework Tutorial Eager Loading

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>&nbsp;</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.
EF Tutorial Eager Loading

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.
The screen will look as:Image may be NSFW.
Clik here to view.
New Product EF Tutorial

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.
EF Eager Loading Example

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.

You are working with WebDevTutorial and building an ADO.NET Entity Framework application for a client. You need to validate the conceptual schema definition language (CSDL), store schema definition language (SSDL), and mapping specification language (MSL) files. Which Entity Data Model tool can you use? (Each correct answer presents a complete solution. Choose all that apply.)
  • 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:

ASP.NET MVC Jobs [Updated Daily]

Top ASP.NET MVC Jobs

Senior Software Engineer
Source: WatchGuard Technologies
Details: C#, ASP.NET, MVC, WCF. WatchGuard, a leader in integrated security platforms, was named “Visionary” in Gartner’s Magic Quadrant for best in class network...  More
12 hours ago

Seattle, WA 98104 29-December-2016

ASP.NET/ANGULAR JS DEVELOPER
Source: Intellisoft Technologies
Details: BlackHawk 700 State Hwy. 121 Bypass Suite 200 Lewisville, Texas 75067 6+ Month Contract Industry leading Client with a great corporate culture, and...  More
14 days ago

Dallas, TX 16-December-2016

ASP.NET/Angular Developer
Source: Intellisoft Technologies
Details: Experience with any MVC - based framework. Is seeking a ASP.NET / Angular Developer for a contract assignment in the Lewisville, TX area!...  More
14 days ago

United States 16-December-2016

Senior Software Engineer
Source: Leafly Holdings
Details: Expert understanding of object oriented design principles and experience applying to large-scale solutions (C# / .NET / ASP.NET MVC preferred)....  More
21 days ago

Seattle, WA 98102 09-December-2016

SDE3(Full stack Developer)
Source: Indeed
Details: 2-4+ years on the following ASP.NET MVC 4 and above. Position - Software Design Engineer 3....  More
8 hours ago

Redmond, WA 29-December-2016

.NET Senior Software Engineer
Source: RDA Corporation
Details: Experience on enterprise projects developing with .NET technologies, such as C#, ASP.NET MVC. RDA is recognized as the leading provider of developing custom...  More
2 days ago

Atlanta, GA 28-December-2016

.Net Developer Senior
Source: Indeed
Details: Strong experience with MVC, rest services and ETL processes. C#, ASP.NET, MVC 5, Entity framework ,HTML 5, CSS, Angular, Modernizer, JavaScript, jQuery, Web API...  More
8 days ago

Roseland, NJ 21-December-2016

Senior Software Engineer
Source: Clarity Consulting Inc.
Details: C# / WinRT / .NET / XAML / Windows 8/8.1 / Windows Phone 8/8.1 / Xbox / UCMA/UCWA / iOS / Cocoa Objective-C / Android / Xamarin / HTML5 / JavaScript/jQuery /...  More
24 days ago

Seattle, WA 06-December-2016

Senior .Net Developer
Source: Indeed
Details: Net MVC applications to different environment. Responsible for deploying ASP .Net and ASP. Top Skills are .Net MVC 4.0 and SQL Server (2008 or 2012 but the team...  More
10 days ago

Dallas, TX 19-December-2016

Senior .Net Developer - Locals only
Source: Indeed
Details:  5+ years building web application with .NET (ASP.NET MVC, Web API, WCF). GC/ US Citizen Only....  More
10 days ago

Atlanta, GA 19-December-2016

Backend Developer II
Source: Indeed
Details: Minimum 4 years experience with writing WinAPI/RESTFul API and working in an ASP.NET MVC environment. Keyventer is seeking a *Back End Web*....  More
2 days ago

Seattle, WA 28-December-2016

C# Software Developer (AngularJS)
Source: CSI Interfusion Inc
Details: Solid skills on C#, ASP.Net, MVC, JavaScript, CSS, HTML, etc. CS I Interfusion ( CSI ) (size:....  More
6 days ago

Redmond, WA 23-December-2016

Senior .Net Developer
Source: Indeed
Details: C# / .Net Development ASP.Net web development experience Windows Server Javascript / Jquery CSS MySQL Server Databases MVC Framework Systems architecture and...  More
13 days ago

New York, NY 16-December-2016

Senior . Net Web Developer with Angular JS
Source: Indeed
Details: MVC. Net Developer with Angular JS:. Senior .Net Web Developer *(Angular JS)*....  More
16 days ago

Harrisburg, PA 13-December-2016

Senior .Net Front End Web Developer
Source: Indeed
Details: You should also know end-to-end MVC application development in .Net. Senior .Net Front End Web Developer*....  More
17 days ago

Houston, TX 12-December-2016

.Net Developer
Source: XDuce
Details: ASP.NET MVC 3.0 / 4.0/ 5.0 5 years. Good understanding of the RPA tool....  More
30+ days ago

Akron, OH 15-November-2016

Senior Software Engineer
Source: Patriot Software
Details: AngularJS, JavaScript , jquery, ASP.NET MVC. Use your extensive programming skills and problem-solving abilities to help Patriot Software, LLC build awesome...  More
16 days ago

Canton, OH 13-December-2016

Web developer(Internal)
Source: Posh Technologies
Details: The product is cloud-based, hosted in Microsoft Azure, using a combination of ASP.NET MVC and Web API, client-side technologies (JS/CSS/HTML), and several...  More
9 days ago

Redmond, WA 21-December-2016

Senior .NET Developer
Source: Indeed
Details: MVC:. ASP.NET MVC and Web Forms. Indeed Hire has partnered with Isagenix....  More
24 days ago

Gilbert, AZ 06-December-2016

Senior Software Engineer
Source: The ScottCare Corporation
Details: Knowledge of new technologies including SignalR, ASP.NET MVC, WebAPI2. Develops software for the next generation of cardiovascular medical device products....  More
17 days ago

Cleveland, OH 44135 12-December-2016

Jr .Net Developer
Source: Indeed
Details: Familiar with Web-API and REST in the ASP.NET MVC world. Professional job experience with C# and ASP.NET MVC (not just ASP.NET web forms). Full time/Long Term*....  More
14 hours ago

Redmond, WA 29-December-2016

Senior Software Engineer, Ed-Fi Alliance - Austin, TX
Source: Michael & Susan Dell Foundation
Details: The Ed-Fi Alliance is currently interviewing for a Senior Software Engineer to lead the efforts to support and enhance its core technology. This role reports  More
22 days ago

Austin, TX 78746 08-December-2016

Senior Software Engineer
Source: Clarity Consulting Inc.
Details: C# / WinRT / .NET / XAML / Windows 8/8.1 / Windows Phone 8/8.1 / Xbox / UCMA/UCWA / iOS / Cocoa Objective-C / Android / Xamarin / HTML5 / JavaScript/jQuery /...  More
24 days ago

Chicago, IL 60606 06-December-2016

Senior Software Developer - .NET/C#
Source: Indeed
Details: JOB DESCRIPTION POSITION TITLE: Senior Developer DEPARTMENT: Development CLASSIFICATION: Exempt REPORTING RELATIONSHIPS POSITION REPORTS TO: Director of  More
30 days ago

Alpharetta, GA 30009 29-November-2016

.Net Developer with Visual Studio 2015, TFS,
Source: Indeed
Details: .Net Developer with Visual Studio 2015, TFS, SQL Server 2014, ASP.Net, MVC, C#, SSIS, SSRS, and SSAS Experience....  More
10 hours ago

Columbia, SC 29-December-2016

The post Entity Framework 6 Eager Loading appeared first on Web Development Tutorial.


Viewing latest article 6
Browse Latest Browse All 15

Trending Articles