A technique that maps an inheritance hierarchy of objects to a single table in relational database.
Questions tagged [table-per-hierarchy]
133 questions
2
votes
1 answer
Is it possible to access a shared TPH column in EF Core without using intermediate classes?
When using shared columns in an EF Core TPH setup, is it possible to access the shared column during projection?
class Program
{
public static readonly ILoggerFactory MyLoggerFactory
= LoggerFactory.Create(builder => {
…

joakimriedel
- 1,801
- 12
- 27
2
votes
1 answer
Entity Framework Core Table-per-Hierarchy: Prevent "unregistered classes" from being added as base type
I need to implement a simple credit system for a web app. So users shall be able to receive credits e.G. by simply paying for them or receive them as a bonus for doing something (like registering, watching an add, etc). On the other hand, the user…

Sven Eppler
- 1,646
- 1
- 15
- 26
2
votes
0 answers
Table Per Hierarchy - Foreign keys on base and derived
I'm not sure if this was possible in EF 6 or earlier. (Current version I use is EF Core 3.1.4) What I'm trying to achieve is to have a base entitiy with some properties and a connection to another entity. From the base entity to have 2 other…

Iosif Bujor
- 89
- 2
- 9
2
votes
1 answer
Possible to call a stored procedure on a Table-Per-Hierarchy table in EF Core 3.1?
I'm moving from EF Core 2.2 to 3.1. One breaking change (#15392) was that it no longer composed over stored procedures, so you had to add 'AsEnumerable'. That usually works, but I have a stored procedure call on a TPH table where that fails:
My…

Jason
- 396
- 1
- 3
- 17
2
votes
1 answer
Entity Framework core 3.1.1 Multilevel Inheritance
An exception is thrown while running add-migration (EF core 3.1.1):
CLR property 'DiscriminatorLevel2Id' cannot be added to entity type 'CustomerBase' because it is declared on the CLR type 'InternalCustomer'
The following Image shows the needed…

Mai ...
- 23
- 1
- 5
2
votes
1 answer
EF4 DB-first: TPH approach?
I know this should not be trivial, but so far couldn't find the resolution...
Working with an EF4 DB-First model, using LINQ-to-Entities with POCOs which will be consumed by an MVC3 app.
I have three entities Customer, CustomerAdress and a lookup…

Eric
- 139
- 2
- 11
2
votes
2 answers
How to change a Domain Class model object from one derived class to another in Grails/GORM
Given the following Grails GORM Domain Classes and using table-per-hierarchy inheritance:
class Book {
static belongsTo = [ parent: Parent ]
String title
}
abstract class Parent {
static hasMany = [ books: Book ]
}
class A extends Parent {
…

Hal Black
- 41
- 3
2
votes
0 answers
Entity Framework table per hierarchy and one to one relation
I am having troubles implementing a table-per-hierarchy architecture in combination with a one-to-one relation in EF 6 code-first and SQL-Server. Entity Framework doesn't use the right column as foreign key.
I have a class Version and two inheriting…

Martin Stimpfl
- 111
- 1
- 8
2
votes
0 answers
Multiple collections with derived classes in parent entity do not work when Table-Per-Hierarchy (TPH) mapping strategy is used by EF6
I have spent better part of the weekend trying to figure this one out.
I have an existing database with Subscription table that I want to map to entities in my domain using TPH inheritance strategy.
There are several types of Subscription, and each…

sacv
- 36
- 4
2
votes
3 answers
Entity Framework:- Error when Casting to derive class throw exception in Table per hierarchy query
I faced an exception when i try to cast to derived class; Unable to cast the type '' to type ''. LINQ to Entities only supports casting EDM primitive or enumeration types.
(obj => ((DerivedClass)obj).DerivedProperty == true);

yo2011
- 971
- 2
- 12
- 38
2
votes
0 answers
Mixing Table-Per-Hierarchy and Table-Per-Type in Entity Framework Code First to Existing Database
tl;dr: I'm trying to map a code-first model to an existing database where a certain hierarchy of objects has a mixed inheritance scheme. Some concrete classes use TPH and some use TPT. I can't seem to get the mapping correct.
I have a hierarchy of…

lc.
- 113,939
- 20
- 158
- 187
2
votes
1 answer
Entity Framework: Table-per-Hierarchy on Multiple Columns
I have set up a view on a legacy table containing order items. In my program, I want to distinguish between phone orders, wifi orders, internet orders, and miscellaneous orders. The legacy table doesn't have a discriminator column, however, you…

Jamie Lester
- 848
- 9
- 20
2
votes
1 answer
Code First EF 6 Table Per Hierarchy SQL query generation issue
I have a model with several classes with inheritance. I have a 4-level hierarchy:
--First level abstrac class A
--Second level abstrac class AA
--Third level:
*abstract class AAA
*abstract class AAB
*abstract class AAC
…

Marcos González
- 172
- 1
- 7
2
votes
1 answer
EF-Migrations FK error when trying to create relationships between subtypes
I my domain model I have a Company base class which is abstract and three different Company types represented by subclasses of Company:
public abstract class Company
{
public int Id { get; set; }
...
}
public class Supplier : Company
{
…

Henrique Miranda
- 1,030
- 1
- 13
- 31
2
votes
1 answer
EF: select all entities of subclass (inheritance)
I am using the Database first approach and DbContext.
I have several inheritance structures (TPH) in my data model.
But DbContext only creates one DbSet for the base class, and none for the subclasses.
How should I retrieve all Entities of a…

ckonig
- 1,234
- 2
- 17
- 29