Working with Inheritances

Entity Framework Core supports two kinds of inheritances: table-per-hierarchy (TPH) and table-per-type (TPT) inheritances. The latter are only supported in Entity Framework Core 5.

In a TPH inheritance hierarchy, data for all the classes in the hierarchy is stored in a single table. The TPH inheritance depends on a conditional mapping which is defined by a condition such as a discriminator database field. This condition is used to define records as different types.

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.

To create an inheritance, use the New Inheritance Images_addInheritance button on the Model toolbar or on the Model Explorer toolbar. Or you can right-click the diagram or class and select New Inheritance from the Add submenu of the class shortcut menu. The Inheritance Editor dialog will be displayed.

Images_InheritanceEditorEFCMF

In Inheritance Editor, you should set the hierarchy type. base and derived classes, and specify discriminator column and parent and child discriminator values.

ExpandedToggleIcon        TPH Example

For example, the database contains the following table:

Images_TPHtable

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

For the purpose of this example, we shall perform the following sequence of actions within the Database-First approach.

Firstly, in the Database Explorer window locate the TPH_Animal table and move it onto the diagram surface to create the TPHAnimal class. At this moment, this class includes properties that are common for all types of animals as well as animal-specific properties.

The common properties are: FoodClassification, BirthDate, Family, and Genus. These properties should remain in the TPHAnimal class, while all the other properties should be moved to the corresponding classes.

Now, we shall start by selecting and moving the snake-related properties (Length and IsAdder) from the TPHAnimal class onto the design surface. This displays the following dialog:

Images_ChooseActionTPH-EFC

In the Model Refactoring dialog, we select New derived class, enter the name of the class (Snake) into the corresponding box and click OK.

Note that the TPH inheritance is created automatically between the Snake class and the TPHAnimal class. To edit this inheritance, we double-click it and, in the Inheritance Editor window, enter the values, as shown below:

Images_INheritanceEditorTPHEFC

Note that the discriminator value specified for the base class is preserved for all other inheritances created with this class. On INSERT, this value is used as the value of the discriminator property column in the database table, while on FETCH allows identifying the corresponding class as the base one.

note Note

In the Discriminator area, select either the Column option button and specify the column, whose values are to be used as the discriminator.

Similarly, we create the Dog class (with the Breed property), the Horse class (with the MaximumSpeed property), and the Crocodile class (with the Weight and Length properties), and edit inheritances that are automatically created between each of the classes and the TPHAnimal class to obtain the final model (see below).

Images_TPH_model_TDA

ExpandedToggleIcon        TPT Example

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

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

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.

Finally, we get the following model:

Images_TPT_Model

 

ExpandedToggleIcon        See Also


Send feedback on this topic

© 2008 - 2024 Devart. All rights reserved.