Junior — Middle
Have you used inheritance principle when working with Entity Framework in your projects?
sobes.tech AI
Answer from AI
In Entity Framework (EF), inheritance is used to model class hierarchies in the database. EF supports several inheritance strategies:
- Table per Hierarchy (TPH): the entire hierarchy is stored in a single table with an additional discriminator column.
- Table per Type (TPT): each class in the hierarchy has its own table, linked through keys.
- Table per Concrete class (TPC): each concrete class has its own table without relationships.
In projects, I used inheritance for convenient representation of entities with common properties. For example, a base class Person with inheritors Employee and Customer. This simplifies the code and allows efficient data handling.
Example:
public class Person {
public int Id { get; set; }
public string Name { get; set; }
}
public class Employee : Person {
public string Position { get; set; }
}
public class Customer : Person {
public string Address { get; set; }
}
In the context of EF, you can configure the inheritance strategy using Fluent API or attributes.