Database Reference
In-Depth Information
The resulting query is somewhat complex; involving several joins and sub-selects. The alternative, leveraging the
default lazy loading behavior of Entity Framework, would require several round trips to the database and could result
in a performance hit, especially if we retrieved many Plumbers.
5-7. Using Include( ) with Other LINQ Query Operators
Problem
You have a LINQ query that uses operators such as group by , join , and where ; and you want to use the Include()
method to eagerly load additional entities. Additionally, you want to implement the Code-First approach for Entity
Framework 6 to manage data access.
Solution
Let's say that you have a model like the one shown in Figure 5-22 .
Figure 5-22. A simple model with a one-to-many association between Club and Event
Start by adding a console application project to Visual Studio entitled Recipe7 . Be certain to reference the Entity
Framework 6 libraries. Leveraging the NuGet Package Manager does this best. Right-click on Reference, and select
Manage NuGet Packages. From the Online tab, locate and install the Entity Framework 6 package. Doing so will
download, install, and configure the Entity Framework 6 libraries in your project.
To create our entity objects, create a class entitled Club and Event and add the code from Listing 5-14.
Listing 5-14. Club Entity Class
public class Club
{
public Club()
{
Events = new HashSet<Event>();
}
public int ClubId { get; set; }
public string Name { get; set; }
public string City { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
 
Search WWH ::




Custom Search