Database Reference
In-Depth Information
2-11. Modeling Is-a and Has-a Relationships Between
Two Entities
Problem
You have two tables that participate in both Is-a and Has-a relationships, and you want to model them as two entities
with the corresponding Is-a and Has-a relationships.
Solution
Let's say that you have two tables that describe scenic parks and their related locations. In your database, you
represent these with a Location table and a Park table. For the purposes of your application, a park is simply a type
of location. Additionally, a park can have a governing office with a mailing address, which is also represented in the
Location table. A park, then, is both a derived type of Location and can have a location that corresponds to the park's
governing office. It is entirely possible that the office is not located on the grounds of the park. Perhaps several parks
share an office in a nearby town. Figure 2-21 shows a database diagram with the Park and Location tables.
Figure 2-21. Location and Park in both a Has-a and Is-a relationship
Follow these steps to model both of these relationships:
1.
Add a new model to your project by right-clicking your project and selecting Add New
Item. Choose ADO.NET Entity Data Model from the Visual C# Data templates.
2.
Select Generate from database. Click Next.
3.
Use the wizard to select an existing connection to your database or create a new
connection.
4.
From the Choose Your Database Object dialog box, select the Location and Park tables.
Leave the Pluralize and Foreign Key options checked. Click Finish.
5.
Delete the one-to-zero or one association created by the Entity Data Model Wizard.
6.
Right-click the Location entity, and select Add Inheritance. Select the Park entity as the
derived entity and the Location entity as the base entity.
7.
Delete the ParkId property from the Park entity type.
8.
Click the Park entity to view the Mapping Details window. If the Mapping Details window
is not visible, show it by selecting View Other Windows Entity Data Model Mapping
Details. Map the ParkId column to the LocationId property.
9.
Change the name of the Location1 navigation property in the Park entity type to Office.
This represents the office location for the park.
 
Search WWH ::




Custom Search