Database Reference
In-Depth Information
The completed model is shown in Figure 2-22 .
Figure 2-22. The completed model with Park deriving from Location. A Park is-a location. A park has-a location
for its office
How It Works
Entities can have more than one association with other entities. In this example, we created an Is-a relationship using
table per type inheritance with Location as the base entity type and Park as the derived entity type. We also created a
Has-a relationship with a one-to-many association between the Location and Park entity types.
In Listing 2-26, we demonstrate creating a new Park entity that also results in creating a Location because of the
Is-a relationship. We attach an office Location to the Park, which results in a second row in the Location table.
Listing 2-26. Creating and Retrieving Park and Location Entities
using (var context = new EF6RecipesContext())
{
var park = new Park { Name = "11th Street Park",
Address = "801 11th Street", City = "Aledo",
State = "TX", ZIPCode = "76106" };
var loc = new Location { Address = "501 Main", City = "Weatherford",
State = "TX", ZIPCode = "76201" };
park.Office = loc;
context.Locations.Add(park);
park = new Park { Name = "Overland Park", Address = "101 High Drive",
City = "Springtown", State = "TX", ZIPCode = "76081" };
loc = new Location { Address = "8705 Range Lane", City = "Springtown",
State = "TX", ZIPCode = "76081" };
park.Office = loc;
context.Locations.Add(park);
context.SaveChanges();
}
 
Search WWH ::




Custom Search