Inheritance Strategy in Entity Framework 6
We have seen in the Code-First Conventions section that EF creates database tables for each concrete domain class. However, you can design your domain classes using inheritance. Object-oriented techniques include "has a" and "is a" relationships, whereas SQL-based relational model has only a "has a" relationship between tables. SQL database management systems don't support type inheritance. So, how would you map object-oriented domain classes with the relational database?
Below are three different approaches to represent an inheritance hierarchy in Code-First:
- Table per Hierarchy (TPH): This approach suggests one table for the entire class inheritance hierarchy. The table includes a discriminator column which distinguishes between inheritance classes. This is a default inheritance mapping strategy in Entity Framework.
- Table per Type (TPT): This approach suggests a separate table for each domain class.
- Table per Concrete Class (TPC): This approach suggests one table for one concrete class, but not for the abstract class. So, if you inherit the abstract class in multiple concrete classes, then the properties of the abstract class will be part of each table of the concrete class.
We are not going into detail here. Visit the following reference links for more detailed information:
- Inheritance with EF Code First: Table per Hierarchy (TPH)
- Inheritance with EF Code First: Table per Type (TPT)
- Inheritance with EF Code First: Table per Concrete class (TPC)
Download EF 6 Code-First Demo Project from Github