EF Core TPH Mapping: Derived Type Property Table Column (2024)

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:

Learn how EF Core handles derived type property mapping in Table Per Hierarchy (TPH) inheritance scenarios.

EF Core TPH Mapping: Derived Type Property Table Column (2024)
Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6270

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.