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 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.
In Inheritance Editor, you should set the hierarchy type. base and derived classes, and specify discriminator column and parent and child discriminator values.
For example, the database contains the following table: 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:
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:
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.
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).
TPT Example
|