Walkthrough: Mapping Table-per-Hierarchy Inheritance in Dynamic Data
This walkthrough shows how to implement table-per-hierarchy (TPH) inheritance by modifying the conceptual model in an Entity Data Model (EDM). The TPH inheritance model will be consumed by a Dynamic Data Entity Framework project.
TPH inheritance uses one database table to maintain data for all the entity types in an inheritance hierarchy. It is also known as single-table inheritance. For more information about how to implement inheritance with an EDM, see Inheritance. For more information about how to use inheritance with Dynamic Data, see ASP.NET Dynamic Data Infrastructure.
In this walkthrough, you will implement TPH inheritance by modifying the EDM that is generated from the School Sample database. In the School Sample database EDM, the Person entity type has two properties, HireDate and EnrollmentDate. These properties can belong to new entity types (Instructor and Student) that inherit from Person.
In the walkthrough, you will implement TPH inheritance using the following steps:
Create two new entity types that derive from Person.
Move properties that are specific to each subtype from the Person entity to the subtype entity.
Set constraints on subtype-specific properties.
Make the Person entity type an abstract type.
Map each entity subtype to the Person table with a condition on a subtype-specific field.
In order to complete this walkthrough, you will need:
Microsoft Visual Studio 2010 or a later version.
The School Sample database. You can download a version of this database for Microsoft SQL Server 2008 from the CodePlex site, or you can create it using Transact-SQL statements. For more information, see Creating the School Sample Database.
A Dynamic Data Entities Web site that uses the School Sample database. This walkthrough assumes that School.edmx is used for the name of the ADO.NET Entity Data Mode (EDM) file.
This walkthrough also assumes that you are familiar with ASP.NET Dynamic Data. For more information, see ASP.NET Dynamic Data.
A Visual Studio project that includes source code is available to accompany this topic: Dynamic Data Entity Framework. A video is available that shows how to add new entities: Mapping Table per Hierarchy Inheritance.
Adding New Entities
To begin, you will modify the conceptual part of the SchoolModel EDM to implement TPH inheritance. You will add two entities, Student and Instructor. Both entities inherit from the Person entity. Properties that are unique to the Student entity will be moved from the Person entity to the Student entity. Properties that are unique to the Instructor entity will be moved from the Person entity to the Instructor entity. The Person entity will contain properties that are shared by both inherited entities.
To add new entities
Open a Dynamic Data Web project that uses the School Sample database and that contains the School.edmx file as the ADO.NET Entity Data Mode (EDM) file.
For more information, see the Prerequisites section of this topic.
In Solution Explorer, double-click the School.edmx file.
The School.edmx file opens in the ADO.NET Entity Data Model Designer (Entity Designer).
Right-click an empty area on the design surface of the Entity Designer, select Add, and then click Entity.
The Add Entity dialog box is displayed. The following illustration shows the UI that is used to add an entity.
The following illustration shows the Add Entity dialog box.
For Entity name, enter Instructor.
From the Base type list,select Person.
The following illustration shows the new entity type that is created and displayed on the design surface.
Repeat the previous steps, but for Entity name, specify Student.
Two new entity types, Instructor and Student, are displayed on the design surface. Arrows point from the new entity types to the Person entity type. This indicates that Person is the base type for the new entity types.
In the Entity Designer, under Scalar Properties, right-click the HireDate property of the Person entity type, and then click Cut.
in the Entity Designer, right-click Scalar Properties for the Instructor entity type and then click Paste.
Right-click the HireDate property and then select Properties.
In the Properties window, set the Nullable property to false.
Repeat the previous four steps, but cut the EnrollmentDate property of the Person entity type and paste it into the Student entity type.
Select the Person entity type.
In the Properties window, set the Abstract property of the Person entity type to true.
A message is displayed that states that defining an entity type as an abstract type will remove all existing function mappings for that type.
You do not have to use abstract types in order to model TPH scenarios. Abstract types are used in this example to demonstrate their use in an EDM.
Mapping the Instructor and Student Entities
You will now map the Instructor and Student entities to the Person entity. Instructors are hired but they do not enroll. Students are not hired, but they enroll. The Entity Framework will use the hire date and enrollment date to discriminate between an Instructor entity and a Student entity.
To map the Instructor and Student entities
If you cannot see the Mapping Details window, right-click the design surface and then click Mapping Details.
Select the Instructor entity type, and then in the Mapping Details window, click <Add a Table or View>.
The following illustration shows the Mapping Details - Instructor dialog box.
The <Add a Table or View> field is redisplayed as a list of tables to which the selected entity can be mapped.
In the list, select Person.
The Mapping Details window is updated with default column mappings and with an option that lets you add a condition.
Click <Add a Condition>.
The <Add a Condition> field is redisplayed as a list of columns for which conditions can be set.
In the list, select HireDate.
In the Operator column of the Mapping Details window, select Is from the list.
In the Property/Value column, select Not Null.
Entity properties can be used only with an Is Null or Is Not Null comparison. They cannot be used in an equals comparison.
The following illustration shows the completed mapping detail.
Repeat the previous steps for the Student entity type, but select the EnrollmenDate property and select the EnrollmentDate Is Not Null condition.
Table-per-hierarchy inheritance is now implemented.
The following illustration shows the table hierarchy inheritance for the entities you have created.
Save and close the School.edmx file.
Testing the Inherited Tables
You can now test the Dynamic Data Web site that you just created.
To test the inherited tables
In Solution Explorer, right-click the Default.aspx page, and then click View in Browser.
The page displays a list that contains the student and instructor tables that you added to the data model. The list does not show the person table because the person table is marked abstract.
Click the student or instructor table.
A page is displayed that contains the data from the student or instructor table. The student table has no Hired column and has an enrollment date in each row. The instructor table has no Enrolled column and has a hired date in each row.
Close the browser.
In this walkthrough, you implemented TPH inheritance in an EDM and displayed the result in a Dynamic Data Entities Web Site. For more information about how to define an EDM with table-per-hierarchy inheritance, see How to: Define a Model with Table-per-Hierarchy Inheritance. For more information about how to implement inheritance with an EDM, see Inheritance (EDM).