Walkthrough: Mapping Table-per-Hierarchy Inheritance in Dynamic Data
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
This topic 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 this walkthrough, you will implement TPH inheritance using the following steps:
-
Create two new entity types that derive from Person.
-
Move properties 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.

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 script.
For more information, see Creating the School Sample Database.
-
A Dynamic Data Entities Web site that uses the School Sample Database.
This sample assumes School.edmx is used for the name of the ADO.NET Entity Data Mode (EDM) file.
This walkthrough assumes that you are familiar with ASP.NET Dynamic Data.
For more information, see ASP.NET Dynamic Data Overview.

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 common to both inherited entities.
See a video that shows how to add new entities as described in the following procedure: Watch.
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.
-
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, point to 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.
-
Click OK.
The following illustration shows the new entity type that is created and displayed on the design surface.
-
Repeat the previous three steps, but for Entity name, enter 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.
-
Under Scalar Properties, right-click the HireDate property of the Person entity type, and then click Cut.
-
Right-click Scalar Properties of 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 abstract type will remove all existing function mappings for that type.
-
Click OK.
Note
|
|
You do not have to use abstract types 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.
Therefore you will add a condition that the hire date cannot be null.
Students are not hired; they enroll.
Therefore you will add a condition to the student entity that the enroll date cannot be null.
The Entity Framework will use the hire date and enroll 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 point to 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 drop-down list of tables to which the selected entity can be mapped.
-
Select Person from the list.
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 drop-down list of columns for which conditions can be set.
-
Select HireDate from the list.
-
In the Operator column of the Mapping Details window, select Is from the drop-down list.
-
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.
-
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.

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.
-
When you have finished, close the browser.

Next Steps
You have successfully 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).
Inheritance (EDM).

See Also
|
チュートリアル: 動的データでの Table-per-Hierarchy 継承のマッピング
[このドキュメントはプレビュー版であり、後のリリースで変更されることがあります。 空白のトピックは、プレースホルダーとして挿入されています。]
このトピックでは、概念モデル、Entity Data Model (EDM) に変更して (TPH) の継承の階層ごとのテーブルを実装する方法を示します。
動的データ Entity Framework プロジェクトで、TPH の継承モデルが使用されます。
TPH の継承では、1 つのデータベース表、継承階層内のすべてのエンティティ型のデータを維持を使用してください。
単一テーブルの継承でとも呼ばれます。
EDM の継承を実装する方法については、Inheritance を参照してください。
動的データで継承を使用する方法については、ASP.NET Dynamic Data のインフラストラクチャ を参照してください。
このチュートリアルでは、学校のサンプル データベースから生成される EDM を変更して TPH 継承実装します。
学校のサンプル データベースの EDM で Person エンティティの種類には 2 つのプロパティ、し、 HireDateEnrollmentDate があります。
これらのプロパティは、エンティティの新しい型 (Instructor と Student) Person から継承されたに属していることができます。
このチュートリアルでは、次の手順を使用して TPH 継承を実装します。
-
Person から派生する 2 つの新しいエンティティ種類を作成します。
-
Person エンティティのサブタイプ エンティティをからには各サブタイプに固有のプロパティを移動します。
-
特定のプロパティ サブタイプに対する制約を設定します。
-
抽象型の入力 Person エンティティを作成します。
-
サブタイプ特定のフィールドの条件を使用して、Person テーブルには、各エンティティのサブタイプがマップされます。

前提条件
このチュートリアルを完了させるには、次の操作が必要です。
-
Microsoft Visual Studio 2010 またはそれ以降のバージョン。
-
学校のサンプル データベースです。
Microsoft SQL Server 2008 はこのデータベースのバージョン、CodePlex サイトからダウンロードできますか Transact-SQL スクリプトを使ってを作成できます。
詳細については、「Creating the School Sample Database」を参照してください。
-
学校のサンプル データベースを使用する動的データ エンティティの Web サイト。
このサンプルは School.edmx です、ADO.NET エンティティ データ モード (EDM) ファイルの名前と仮定します。
このチュートリアルではであると見なす ASP.NET の動的なデータ操作に慣れて。
詳細については、「ASP.NET Dynamic Data の概要」を参照してください。

新しいエンティティの追加
開始するは TPH の継承を実装する SchoolModel EDM の概念の部分を変更します。
2 つのエンティティ、StudentInstructor を追加します。
両方のエンティティは、Person エンティティによって継承します。
Student エンティティに対して一意されるプロパティは Person エンティティに Student エンティティから移動されます。
Instructor エンティティに対して一意されるプロパティは Person エンティティに Instructor エンティティから移動されます。
Person エンティティには、両方の継承されたエンティティに共通のプロパティです。
次の手順に従って新しいエンティティを追加する方法について説明するビデオを参照してください。 Watch.
新しいエンティティを追加するには
-
学校のサンプル データベースを使用する動的なデータ Web プロジェクトを開き、ADO.NET エンティティ データ モード (EDM) ファイルとして School.edmx ファイルを含みます。
-
のソリューション エクスプローラー] で School.edmx ファイルをダブルクリックします。
ADO.NET エンティティ データ モデル デザイナー (エンティティ デザイナー) では、School.edmx ファイルが開きます。
-
エンティティ デザイナーのデザイン サーフェイスに空の領域を右クリックして を追加] をポイントして [エンティティ をクリックします。
エンティティの追加 の] ダイアログ ボックスが表示されます。
次の図には、エンティティの追加に使用されるの UI 示しています。
次の図は、エンティティの追加 ダイアログ ボックスです。
-
講師用 エンティティ名 を入力します。
-
From the Base type list,select Person.
-
[OK] をクリックします。
次に作成され、デザイン サーフェイスに表示される新しいエンティティの種類を示します。
-
前の 3 つ手順を繰り返してが入力受講者用 エンティティ名。
新しいエンティティと 2 種類、講師用の 受講者用 はデザイン サーフェイスに表示します。
新しいエンティティ型から 人 エンティティの種類をポイントして矢印します。
ユーザー その が、新しいエンティティの種類の基本型をこのを示します。
-
のスカラー プロパティ、 HireDate エンティティの種類の Person プロパティを右クリックし、 をカットしています。
-
エンティティ型のスカラー プロパティ Instructor を右クリックし、 の貼り付けを実行します。
-
HireDate プロパティを右クリックし、 のプロパティを [します。
-
[プロパティ] ウィンドウで、Nullable プロパティを false に設定します。
-
前の 4 つ手順を繰り返してが EnrollmentDate エンティティの種類の Person プロパティを切り取りし、 Student エンティティの種類に貼り付けます。
-
Person エンティティ タイプを選択します。
-
プロパティ ウィンドウでに AbstractPerson エンティティの種類の true プロパティを設定します。
抽象型はその型のすべての既存関数マッピングを削除すると、エンティティの種類を定義することを示すメッセージが表示されます。
-
[OK] をクリックします。
メモ
|
|
モデル TPH のシナリオを抽象型を使用する必要はありません。
抽象型を使用この例では、EDM の用途を説明します。
|

講師用および受講者用のエンティティのマッピング
Instructor
Student エンティティ、今すぐ Person エンティティにマップされます。
教官は、雇用が登録はできません。
したがって、入社日できない null 条件が追加されます。
受講者は採用しない;登録します。
したがって、条件の登録日付 null ができない受講者用エンティティに追加します。
Entity Framework 入社日を使用し、Instructor エンティティと、 Student エンティティを区別する日付を登録します。
講師用および受講者用のエンティティをマップするには
-
マッピングの詳細] ウィンドウが表示することはできない場合、デザイン サーフェイスを右クリックし、 のマッピングの詳細をポイントします。
-
Instructor エンティティ タイプを選択し、 マッピングの詳細の ウィンドウで < 追加テーブルまたはビューを > 。
次の図は、マッピングの詳細 - 台目の講師用 ダイアログ ボックスです。
< 追加、テーブルまたはビュー > フィールドが選択したエンティティがマップ先テーブルのドロップダウン リストとして表示されます。
-
一覧からユーザー を選択します。
マッピングの詳細 ウィンドウには既定の列マッピングと条件を追加でくオプションを更新がします。
-
クリックして < 追加条件 >。
< 追加条件 > フィールドが条件を設定できる列のドロップダウン リストとして表示されます。
-
一覧から HireDate を選択します。
-
オペレーター 列マッピングの詳細 ウィンドウの ドロップ ボックスの一覧からです。
-
プロパティの値のの 列で、Not Null を選択します。
メモ
|
|
エンティティのプロパティは、Is Null を または Is Not Null 比較でのみ使用できます。
これらは、等値比較では使用できません。
|
次に、完成したマッピングの詳細を示します。
-
Student エンティティの種類、に対して上記の手順を繰り返しますが EnrollmenDate プロパティを選択し、 の EnrollmentDate Is Not Null の条件を選択します。
階層ごとのテーブルの継承は実装されています。

継承されたテーブルのテスト
ここでは、作成した Dynamic Data Web サイトをテストします。
継承された、テーブルをテストするには
-
ソリューション エクスプローラーで、Default.aspx ページを右クリックし、[ブラウザーで表示] をクリックします。
ページには、受講者と講師テーブル データ モデルに追加したを含むリストが表示されます。
一覧は、ユーザー テーブルが抽象マークいるので、担当者テーブルを表示されません。
-
受講者用または教官テーブルをクリックします。
受講者用または教官テーブルからデータを含むページが表示されます。
受講者用テーブルには"Hired"はありません。列各行に、登録の日付であり。
講師用テーブルには"Enrolled"はありません。列各行の採用を日付であり。
-
完了したら、ブラウザーを閉じます。

次の手順

参照
|