Table per hierarchy

Table Per Hierarchy (TPH) inheritance (also called Flat inheritance in Telerik Data Access terminology) is the most common and easiest type of inheritance. In TPH, an entire inheritance tree is created through a single table that includes a discriminator field. This discriminator field determines which specific type each record represents.

The TPH inheritance depends on conditional mapping defined by a discriminator database field. This condition is used to identify records as different types.

Consider a database table containing animal information:

Animal table with ClassType discriminator

In this table, ClassType is the discriminator (either a string or integer) that determines the specific type of animal.

Creating a TPH inheritance (Database-First approach)

Follow these steps to create a TPH inheritance from an existing database table.

Step 1: create the base class

  1. In the Database Explorer window, locate the table (in this example, TPH_Animal).
  2. Drag it onto the diagram surface to create the base class (TPHAnimal).

The class will initially include all properties (common and animal-specific).

Step 2: identify common properties

Identify which properties are common to all types:

  • Common properties (stay in base class): FoodClassification, BirthDate, Family, Genus.
  • Animal-specific properties (move to derived classes): Length, Breed, MaximumSpeed, Weight, etc.

Step 3: create derived classes

Select and drag animal-specific properties from the base class onto the design surface. For example:

  1. Drag snake-related properties (Length and IsAdder) to create a Snake class.
  2. The Model Refactoring dialog appears.
  3. Select New derived class and enter the class name.
  4. Click OK.

Model Refactoring dialog for creating derived classes

Step 4: configure discriminator values

The TPH inheritance is created automatically between derived and base classes. To edit:

  1. Double-click the inheritance relationship.
  2. In the Inheritance Editor, configure the discriminator value for each class.

Inheritance Editor for TPH

Note

The discriminator value specified for the base class is preserved for all other inheritances created with this class. On INSERT, this value is used in the discriminator column. On FETCH, it identifies the record as the base type.

Step 5: create additional derived classes

Repeat the process for other animal types:

  • Dog class (with Breed property).
  • Horse class (with MaximumSpeed property).
  • Crocodile class (with Weight and Length properties).

Edit each automatically created inheritance to set the appropriate discriminator values.

The resulting model structure:

Completed TPH model with all derived classes

Discriminator configuration

In the Inheritance Editor, you can configure the discriminator:

  • Select the Column option and specify the database column whose values are used as the discriminator.
  • Enter the discriminator value for each derived class.

See also

  • Overview
  • Table per type
  • Table per concrete type
  • Working with inheritances