Sobes.tech
Junior — Middle

How does Entity Framework implement the mapping of C# inheritance classes into the database table structure?

sobes.tech AI

Answer from AI

Entity Framework supports several inheritance mapping strategies for C# classes to database tables:

  1. Table Per Hierarchy (TPH) — all classes and their properties are stored in a single table with an additional discriminator column indicating the object type.

  2. Table Per Type (TPT) — a separate table is created for each class, where derived tables contain only additional fields, and the relationship with the base class is maintained through a foreign key.

  3. Table Per Concrete Class (TPC) — a separate table is created for each concrete class, containing all fields, including inherited ones.

Example of TPH:

public abstract class Animal { public int Id { get; set; } public string Name { get; set; } }
public class Cat : Animal { public int Lives { get; set; } }
public class Dog : Animal { public string Breed { get; set; } }

// In the database, there will be one table Animals with columns Id, Name, Lives, Breed, and Discriminator

The choice of strategy depends on performance requirements and data structure.

How does Entity Framework implement the mapping of C#… - sobes.tech