Compartir a través de


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:

  1. Create two new entity types that derive from Person.

  2. Move properties that are specific to each subtype from the Person entity to the subtype entity.

  3. Set constraints on subtype-specific properties.

  4. Make the Person entity type an abstract type.

  5. Map each entity subtype to the Person table with a condition on a subtype-specific field.

Prerequisites

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

  1. 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.

  2. In Solution Explorer, double-click the School.edmx file.

    The School.edmx file opens in the ADO.NET Entity Data Model Designer (Entity Designer).

  3. 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.

    Right-click on an empty space of the design surfac

    The following illustration shows the Add Entity dialog box.

    Add Entity

  4. For Entity name, enter Instructor.

  5. From the Base type list,select Person.

  6. Click OK.

    The following illustration shows the new entity type that is created and displayed on the design surface.

    The Instructor Entity

  7. 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.

  8. In the Entity Designer, under Scalar Properties, right-click the HireDate property of the Person entity type, and then click Cut.

  9. in the Entity Designer, right-click Scalar Properties for the Instructor entity type and then click Paste.

  10. Right-click the HireDate property and then select Properties.

  11. In the Properties window, set the Nullable property to false.

  12. Repeat the previous four steps, but cut the EnrollmentDate property of the Person entity type and paste it into the Student entity type.

  13. Select the Person entity type.

  14. 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.

  15. Click OK.

    Note

    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

  1. If you cannot see the Mapping Details window, right-click the design surface and then click Mapping Details.

  2. 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.

    Select the Instructor entity type and click <Add>

    The <Add a Table or View> field is redisplayed as a list of tables to which the selected entity can be mapped.

  3. 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.

  4. Click <Add a Condition>.

    The <Add a Condition> field is redisplayed as a list of columns for which conditions can be set.

  5. In the list, select HireDate.

  6. In the Operator column of the Mapping Details window, select Is from the list.

  7. In the Property/Value column, select Not Null.

    Note

    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.

    Completed Mapping Detail

  8. 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.

    Display table hierarchy

  9. 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

  1. 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.

  2. 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.

  3. Close the browser.

Next Steps

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).

See Also

Concepts

ASP.NET Dynamic Data

Other Resources

Object Relational Designer (O/R Designer)

LINQ to SQL

ADO.NET Entity Framework