Table Per Type

A Table Per Type (TPT) inheritance is an inheritance that is represented in the database by several separate tables. Every such table contains additional details describing a new type based on another table that is the parent of that table.

In this example, we shall use the Database-First approach.

Suppose that there are the following linked tables in the database:

Images_TPT_DB

We move all these tables from the Database Explorer window onto the design surface. If the Detect Table per Type inheritances check box was selected in model settings, the hierarchy is generated automatically, and no further actions are required. If no, the model is generated as follows:

images_TPT-Model-Before-EF

The TPT inheritance for this model can be created in two ways, i.e. manually, with the Inheritance editor, or via Model Refactoring Wizard.

Creating Inheritance via Wizard

After all the required classes are added to the diagram area, we select them, call their context menu, and select the Model Refactoring menu item.

The Choose Action page of the Model Refactoring Wizard is displayed:

images_refactoring-wizard-TPT

On this page we select the Table-Per-Type Hierarchy (TPT) option and click Finish.

Manual creation of the inheritance

Now we need to remove manually the available associations and create correctly defined TPT inheritances, so we delete the associations and primary key properties of all derived classes one by one (and at the same time keep the primary key property of the TPTAnimal base class), and then create the corresponding inheritances via the Inheritance Editor dialog box:

images_Inheritance-Editor-TPT-EF

After the inheritances are created, we map the ID columns of all derived classes to the ID property of the TPTAnimal base class via the Entity Mapping Details dialog box (for details see Entity Mapping).

 

Thus, we get the classes connected with TPT inheritance; excessive navigation properties are removed from all classes, and the excessive primary key properties are removed from the derived classes automatically.

The result we get is the same irrespective of the approach to inheritance creation we use, but automatic inheritance creation takes fewer steps. For more information on automatic inheritance creation and other model refactoring options, see Model Refactoring Wizard.

Finally, we get the following model:

Images_TPT_Model

 

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.