Abstract: In Entity Framework Core (EF Core), Table Per Hierarchy (TPH) mapping is used when we have a table that contains data for multiple derived types. In this article, we will discuss how EF Core decides to map properties to columns in such a scenario.
2024-06-04 by DevCodeF1 Editors
EFCore TPH Mapping: Derived Type Property Table Column
In this article, we will discuss the Table Per Hierarchy (TPH) inheritance mapping strategy in EF Core, focusing on how derived type properties are mapped to table columns. By the end of this article, you will have a solid understanding of TPH inheritance mapping and how EF Core decides which properties and columns to map.
Introduction to TPH Inheritance Mapping
In EF Core, TPH is a type of inheritance mapping strategy where a single table is used to store all the entities in the hierarchy. Each entity type in the hierarchy is represented by a discriminator column in the table, which EF Core uses to identify the type of each row. This approach has several advantages, such as reduced database joins and simpler queries. However, it also has some limitations, such as less efficient storage of large object graphs and potential performance issues with complex queries.
Derived Type Property Table Column Mapping
When using TPH inheritance mapping in EF Core, each derived type's properties are mapped to columns in the same table as the base type. EF Core decides which columns to map based on the property names and types. By default, EF Core will map all properties with the same name and type to the same column. However, you can also use the [Column]
attribute to specify a different column name for a property, as shown in the following example:
public abstract class Vehicle {public int Id { get; set; }[Column("VehicleType")]public string Type { get; set; }}public class Car : Vehicle {public int NumberOfDoors { get; set; }}public class Motorcycle : Vehicle {public bool HasSidecar { get; set; }}
In this example, the Type
property of the Vehicle
class is mapped to a column named VehicleType
, while the NumberOfDoors
property of the Car
class and the HasSidecar
property of the Motorcycle
class are mapped to separate columns in the same table.
Discriminator Column
The discriminator column is a special column in the TPH table that EF Core uses to identify the type of each row. By default, EF Core uses a string value of the derived type's name as the discriminator value. However, you can also use the [Discriminator]
attribute to specify a different value or type for the discriminator column, as shown in the following example:
public abstract class Vehicle {[Discriminator]public int VehicleTypeId { get; set; }public int Id { get; set; }public string Type { get; set; }}[Discriminator(nameof(VehicleTypeId), typeof(int))]public class Car : Vehicle {public int NumberOfDoors { get; set; }}[Discriminator(nameof(VehicleTypeId), typeof(int))]public class Motorcycle : Vehicle {public bool HasSidecar { get; set; }}
In this example, the VehicleTypeId
property of the Vehicle
class is used as the discriminator column, with an integer value indicating the type of the vehicle. The Car
and Motorcycle
classes also use the VehicleTypeId
property as the discriminator column, with different integer values indicating their respective types.
In this article, we have discussed the TPH inheritance mapping strategy in EF Core, focusing on how derived type properties are mapped to table columns. We have covered the basics of TPH inheritance mapping, as well as the use of the [Column]
and [Discriminator]
attributes to customize the mapping. By understanding these concepts, you can effectively use TPH inheritance mapping in your EF Core applications.
References
Types of references:
- Online resources: Inheritance in EF Core, Relational inheritance in EF Core, Data annotations in EF Core
Learn how EF Core handles derived type property mapping in Table Per Hierarchy (TPH) inheritance scenarios.
Resizing and Converting Bitmatrix with Threshold Dithering in JavaScript for Image Upload
Learn how to resize and convert a bitmatrix using threshold dithering in JavaScript for uploading images to a website.
Linear Mixed Model: Model Creation Plots for Echocardiography Data
This article discusses the creation of model plots for Linear Mixed Models using R, focusing on echocardiography data containing multiple measurements per patient over time.
Preventing Inheritance of navbar-expand-lg Class in Nested Navbars
In a situation where two navbars exist, one nested inside the other, preventing the inner navbar from inheriting the navbar-expand-lg class can be achieved by adding custom CSS.