https://github.com/eliasprates/inheritanceefcore
Inheritance in EF Core - Herança no EF Core - TPH - TPC -TPT - Fluent API
https://github.com/eliasprates/inheritanceefcore
dependency-injection dotnet entity-framework-core fluent-api inheritance tpc tph tpt
Last synced: 3 months ago
JSON representation
Inheritance in EF Core - Herança no EF Core - TPH - TPC -TPT - Fluent API
- Host: GitHub
- URL: https://github.com/eliasprates/inheritanceefcore
- Owner: eliasprates
- License: mit
- Created: 2024-01-28T09:55:49.000Z (over 1 year ago)
- Default Branch: master
- Last Pushed: 2024-01-28T11:29:06.000Z (over 1 year ago)
- Last Synced: 2025-03-04T19:31:34.331Z (7 months ago)
- Topics: dependency-injection, dotnet, entity-framework-core, fluent-api, inheritance, tpc, tph, tpt
- Language: C#
- Homepage: https://learn.microsoft.com/en-us/ef/core/modeling/inheritance
- Size: 14.6 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# (PT-BR)
# Estratégias de Herança no Entity Framework CoreEste repositório demonstra as diferentes abordagens de herança utilizadas no Entity Framework Core: Table Per Hierarchy (TPH), Table Per Type (TPT) e Table Per Concrete class (TPC). Cada abordagem tem suas próprias características e é adequada para diferentes cenários.
## Table Per Hierarchy (TPH)
Na abordagem TPH, todas as classes na hierarquia de herança são mapeadas para uma única tabela. A tabela possui uma coluna discriminadora para diferenciar entre os tipos.
### Estrutura da Tabela TPH
#### Tabela: Vehicles
| Column | Type | Notas |
|---------------|---------|----------------------------------|
| Id | int | Chave Primária |
| Model | string | |
| Year | int | |
| Type | string | Discriminador (Car, Motorcycle) |
| NumberOfDoors | int | NULL para motocicletas |
| HasFairing | boolean | NULL para carros |## Table Per Type (TPT)
A abordagem TPT mapeia cada classe na hierarquia de herança para sua própria tabela, com cada classe derivada tendo sua própria chave primária e uma chave estrangeira referenciando a tabela da classe base.
### Estrutura das Tabelas TPT
#### Tabela: Vehicles
| Column | Type | Notas |
|---------------|---------|----------------------------------|
| Id | int | Chave Primária |
| Model | string | |
| Year | int | |#### Tabela: Cars
| Column | Type | Notas |
|---------------|---------|----------------------------------|
| Id | int | Chave Primária |
| VehicleId | int | Chave Estrangeira para Vehicles |
| NumberOfDoors | int | |#### Tabela: Motorcycles
| Column | Type | Notas |
|---------------|---------|----------------------------------|
| Id | int | Chave Primária |
| VehicleId | int | Chave Estrangeira para Vehicles |
| HasFairing | boolean | |## Table Per Concrete class (TPC)
Na abordagem TPC, cada classe concreta na hierarquia de herança é mapeada para sua própria tabela, e a classe base não é representada.
### Estrutura das Tabelas TPC
#### Tabela: Cars
| Column | Type | Notas |
|---------------|--------|----------------------------------|
| Id | int | Chave Primária |
| Model | string | |
| Year | int | |
| NumberOfDoors | int | |#### Tabela: Motorcycles
| Column | Type | Notas |
|---------------|---------|----------------------------------|
| Id | int | Chave Primária |
| Model | string | |
| Year | int | |
| HasFairing | boolean | |---
Para mais informações sobre como implementar cada uma dessas abordagens no Entity Framework Core, consulte a documentação oficial.
# (EN-US)
# Inheritance Strategies in Entity Framework CoreThis repository demonstrates the different inheritance approaches used in Entity Framework Core: Table Per Hierarchy (TPH), Table Per Type (TPT), and Table Per Concrete class (TPC). Each approach has its own characteristics and is suitable for different scenarios.
## Table Per Hierarchy (TPH)
In the TPH approach, all classes in the inheritance hierarchy are mapped to a single table. The table has a discriminator column to differentiate between the types.
### TPH Table Structure
#### Tabela: Vehicles
| Column | Type | Notes |
|---------------|---------|----------------------------------|
| Id | int | Primary Key |
| Model | string | |
| Year | int | |
| Type | string | Discriminator (Car, Motorcycle) |
| NumberOfDoors | int | NULL for motorcycles |
| HasFairing | boolean | NULL for cars |## Table Per Type (TPT)
The TPT approach maps each class in the inheritance hierarchy to its own table, with each derived class having its own primary key and a foreign key referencing the base class table.
### TPT Table Structures
#### Table: Vehicles
| Column | Type | Notes |
|---------------|---------|----------------------------------|
| Id | int | Primary Key |
| Model | string | |
| Year | int | |#### Table: Cars
| Column | Type | Notes |
|---------------|---------|----------------------------------|
| Id | int | Primary Key |
| VehicleId | int | Foreign Key to Vehicles |
| NumberOfDoors | int | |#### Table: Motorcycles
| Column | Type | Notes |
|---------------|---------|----------------------------------|
| Id | int | Primary Key |
| VehicleId | int | Foreign Key to Vehicles |
| HasFairing | boolean | |## Table Per Concrete class (TPC)
In the TPC approach, each concrete class in the inheritance hierarchy is mapped to its own table, and the base class is not represented.
### TPC Table Structures
#### Table: Cars
| Column | Type | Notes |
|---------------|---------|----------------------------------|
| Id | int | Primary Key |
| Model | string | |
| Year | int | |
| NumberOfDoors | int | |#### Table: Motorcycles
| Column | Type | Notes |
|---------------|---------|----------------------------------|
| Id | int | Primary Key |
| Model | string | |
| Year | int | |
| HasFairing | boolean | |---
For more information on how to implement each of these approaches in Entity Framework Core, consult the official documentation.