Database Reference
In-Depth Information
Figure 8-3. A model representing venues, their events, and the competitors in the events
We're using POCO for our entities, and we want to eagerly load the related entities (navigation properties).
To do this, we use the Include() method available on the object context. The code in Listing 8-4 illustrates using the
Include() method to do this.
Listing 8-4. Using the Include() Method Explicitly to Load Navigation Properties
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
using (var context = new EFRecipesEntities())
{
var venue = new Venue { Name = "Sports and Recreational Grounds" };
var event1 = new Event { Name = "Inter-school Soccer" };
event1.Competitors.Add(new Competitor { Name = "St. Mary's School" });
event1.Competitors.Add(new Competitor { Name = "City School" });
venue.Events.Add(event1);
context.Venues.Add(venue);
context.SaveChanges();
}
using (var context = new EFRecipesEntities())
{
foreach (var venue in context.Venues.Include("Events").Include("Events.Competitors"))
{
Console.WriteLine("Venue: {0}", venue.Name);
foreach (var evt in venue.Events)
{
Console.WriteLine("\tEvent: {0}", evt.Name);
Console.WriteLine("\t--- Competitors ---");
foreach (var competitor in evt.Competitors)
 
Search WWH ::




Custom Search