Skip to content

Relational: TPT inheritance mapping pattern #2266

Description

@satyajit-behera

Even if TPT is considered slow, it is a boon for many real world business scenarios. If implemented properly and well thought of, TPT is not slow for a given need in most of the cases.

If possible, optimization can be done to TPT.
But its a very important feature for EF to be accepted for developing DDD applications.

The other method, Composition over Inheritance does not look viable since we cannot use interface property to create our model. Binding to a concrete type takes away the flexibility to customize the models. TPT makes the customization very easy and flexible.

Activity

  1. added this to the Backlog milestone on May 29, 2015
  2. rowanmiller commented on May 29, 2015

    @rowanmiller
    Contributor

    Update: we are planning to implement TPT. This comment is from 2015. Please read the rest of the thread, not just this comment.

    The feeling from our team is that TPT is generally an anti-pattern and results in significant performance issues later on. While enabling it may make some folks "happier" to start with it ultimately just leads to issues. We are willing to consider it though, so we're leaving this open and will consider it based on the feedback we get.

  3. satyajit-behera commented on May 29, 2015

    @satyajit-behera
    Author

    Thanks. Issue is the absence of any alternative to build an OO application. Even Composition using interfaces does not look viable for now.

  4. rowanmiller commented on May 29, 2015

    @rowanmiller
    Contributor

    We are in the process of implementing TPH at the moment, so you will be able to persist an inheritance hierarchy to a single table in the near future.

  5. satyajit-behera commented on May 29, 2015

    @satyajit-behera
    Author

    But it does not solve the main issue of extending a table. TPT becomes a necessity in practical business scenario. Otherwise, it may become a table with 100s of fields in it.

  6. anpete commented on May 29, 2015

    @anpete
    Contributor

    @satyajit-behera TPT is an inheritance mapping pattern. Are you referring to "Entity Splitting". i.e. Having a single entity mapped to more than one table?

  7. satyajit-behera commented on May 30, 2015

    @satyajit-behera
    Author

    @anpete I am referring to One table per inherited entity type.

  8. GSPP commented on Jun 15, 2015

    @GSPP

    TPT can be important for performance. Think of huge tables. Usually, you want only the absolutely required fields in such a table. TPH has column bloat, TPT is lean. Also, with TPT database statistics can be specialized for each type. This is important because different types can have vastly different data distributions.

    TPT is quite an important pattern and I don't see why it would be considered an anti-pattern. An anti-pattern is a thing that is almost always the wrong choice. This does not seem to hold here.

  9. santanaguy commented on Jun 23, 2015

    @santanaguy

    @GSPP i agree with you. Having TPT is a must for an ORM and i think it was recurrent solution for a lot of problems in EF6 in the real world. I used it in my models a lot. One should be careful with the tools he uses, but that doesn't mean he shouldn't have those tools available. Both types of inheritance should be present in EF Core. Please include this scenario in the 7.0 release

  10. satyajit-behera commented on Jul 3, 2015

    @satyajit-behera
    Author

    @rowanmiller I hope you will prioritize and include the TPT inheritance mapping for EF 7. All of us can discuss if some specific feature make it slow and get a workaround for the same. But the feature itself is very useful in business applications. Please include this in EF Core release. Thanks a lot.

  11. ToddThomson commented on Jul 16, 2015

    @ToddThomson

    👍 TPT inheritance may have some performance issues, but the design clarity it provides for real world scenarios outweighs them. I also do not feel that TPT inheritance is an "anti-pattern".

  12. konstantin999 commented on Jul 31, 2015

    @konstantin999
  13. konstantin999 commented on Jul 31, 2015

    @konstantin999
  14. AndyNewland commented on Aug 27, 2015

    @AndyNewland

    Agree 100% with @ToddThomson. This is another strong vote to add this feature ASAP. I think that TPH can be viewed as a SQL Server anti-pattern once the number of columns becomes unmanageable from a statistics and (more importantly) indexing viewpoint.

  15. 314 remaining items

  16. boomalator commented on Aug 23, 2020

    @boomalator

    is that at some point you could have multiple Teachers/Students for one Person. By using the following design instead:

    That happens in the real world, though. In my last year of college, I was a STUDENT in my courses, but also hired to be a TEACHER in the Cont Ed division (teaching AutoCad and Excel to senior citizens... tons of fun).

    While a bird is not going to be both an Eagle and and a Kiwi, a person is totally able to be both a Student and a Teacher. Another example is a well-known online accounting system that doesn't allow a person (or a company) to be more that one of "supplier", "customer" and "employee", which means multiple records when a staff member has to be paid for some odd expense, or you both buy from and sell to another company.

    The real world answer tends to be creating a "John Smith (Student)" and "John Smith (Teacher)" record, but that is confusing and leads to errors (when John moves and only one address is updated).

    What's the right way to model those kinds of relationships?

  17. gojanpaolo commented on Aug 23, 2020

    @gojanpaolo

    @boomalator You could design it such that a Person (John Smith) can have many Jobs (Student and Teacher). It might be a many-to-many relationship because multiple Person can have the same Job.

  18. boomalator commented on Aug 23, 2020

    @boomalator

    @boomalator You could design it such that a Person (John Smith) can have Jobs (Student and Teacher). It might be a many-to-many relationship because multiple Person can have the same Job.

    That would suggest to me that modeling Student and Teacher as inheriting from Person would be flawed as a practice and as an example.

  19. gojanpaolo commented on Aug 23, 2020

    @gojanpaolo

    For simple cases and examples, inheritance will work just fine. But it doesn't mean that we need to keep modeling it as inheritance when the scope becomes bigger than what it was initially designed for. It also doesn't mean that we should avoid using inheritance at all. It really depends on the domain you're working on. 😀

  20. marchy commented on Aug 23, 2020

    @marchy

    is that at some point you could have multiple Teachers/Students for one Person. By using the following design instead:

    That happens in the real world, though. In my last year of college, I was a STUDENT in my courses, but also hired to be a TEACHER in the Cont Ed division (teaching AutoCad and Excel to senior citizens... tons of fun).

    While a bird is not going to be both an Eagle and and a Kiwi, a person is totally able to be both a Student and a Teacher. Another example is a well-known online accounting system that doesn't allow a person (or a company) to be more that one of "supplier", "customer" and "employee", which means multiple records when a staff member has to be paid for some odd expense, or you both buy from and sell to another company.

    The real world answer tends to be creating a "John Smith (Student)" and "John Smith (Teacher)" record, but that is confusing and leads to errors (when John moves and only one address is updated).

    What's the right way to model those kinds of relationships?

    This is a wonderful question @boomalator.

    This comes up a lot at Facebook for example, where their Graph API models everything as Nodes (ie: entities) and Edges (ie: relationships), as well as Fields which store the data about those entities/relationships. These are used to model everything including People, Organizations, Pages, Events, Groups etc.

    While you don't necessarily have to abstract your entire data base into an object graph like Facebook does, the moment you start hitting 'roles' that a person can take on (ie: them being a Student, a Teacher, a Father, Investor etc.)... inheritance is likely a misfit for modelling the real-world structure at hand.

    Think about the domain you are trying to model and try to pick the right level of abstraction. Is it a Position that might have a title, length of employment etc. (such as for recruiting/LinkedIn), or something more/less abstract like a Job (ie: job would not appropriately account for a sitting on an executive board or volunteering for something, as those are not technically 'jobs').

  21. PieterjanDeClippel commented on Aug 24, 2020

    @PieterjanDeClippel
    Contributor

    is that at some point you could have multiple Teachers/Students for one Person. By using the following design instead:

    That happens in the real world, though. In my last year of college, I was a STUDENT in my courses, but also hired to be a TEACHER in the Cont Ed division (teaching AutoCad and Excel to senior citizens... tons of fun).

    While a bird is not going to be both an Eagle and and a Kiwi, a person is totally able to be both a Student and a Teacher. Another example is a well-known online accounting system that doesn't allow a person (or a company) to be more that one of "supplier", "customer" and "employee", which means multiple records when a staff member has to be paid for some odd expense, or you both buy from and sell to another company.

    The real world answer tends to be creating a "John Smith (Student)" and "John Smith (Teacher)" record, but that is confusing and leads to errors (when John moves and only one address is updated).

    What's the right way to model those kinds of relationships?

    Okay, indeed not such a good example. The example of Bird <- Kiwi and Bird <- Eagle is more applicable if you're talking about Inheritance. Or another example:

    Vehicle <- Car
            <- Bike
            <- Motorbike
    

    The case you're talking about, eg.

    Member <- Customer
           <- Supplier
           <- Contact
    

    Where a Member can be both Customer, Supplier and Contact is not an example of inheritance, but can be modelled like this:

    public class Member
    {
        public List<Customer> Customers { get; set; }
        public List<Supplier> Suppliers { get; set; }
        public List<Contact> Contacts { get; set; }
    }
    
    public class Customer
    {
        public Member Member { get; set }
    }
    
    public class Supplier
    {
        public Member Member { get; set }
    }
    
    public class Contact
    {
        public Member Member { get; set }
    }
    

    Then a member can be both Customer and Supplier

  22. AndriySvyryd commented on Oct 10, 2020

    @AndriySvyryd
    Member

    @RonOsmo Please file those as separate issues with small runnable projects that show the unwanted behavior

  23. modified the milestones: , 5.0.0 on Nov 7, 2020
  24. kimballjohnson commented on May 16, 2021

    @kimballjohnson

    Sorry, but I'm not seeing a clear story here for either the 'for' or the 'against' argument regarding TPT.

    Certainly, the obvious use case here has to be the PostgreSQL hierarchical table arrangement.
    While I understand the natural inclination for a Microsoft framework is to advance integration scenarios that support well-established database platforms.

    But discussions of samples and conceptual representations of TPT as a query-syntax-structure problem rather than a data-entity-relationship challenge seem intentionally designed to avoid the value of hierarchical tables in favor of the TPT implementation pattern (or style, depending on how it is conceived).

    My viewpoint is that those posting requests for TPT are making a DB design and implementation argument in favor of the established value of the hierarchical table use case.

    But the perspective of the EF team is that all issues are aspects of the conceptual framework of TPH/TPT and its variants.

    All of these are implementation issues, not use cases, and that perspective drives this discussion, and the decision it should be targeting toward basing this fundamental framework design choice exclusively on what is essentially a SQL Query Optimization decision tree.

    I believe the correct elucidation of the TPT question, and the path to the answer lies in an examination of the source code of PostgreSQL Hierarchical Table implementation, since none of the requesters are expressing a requirement based on a modified or replaced TPT strategy, but are looking for the data design and entity-relationship efficiency that isolates Application-side Entities from the repetitive server-side utility and tracking data tables that are attached by means of relations to true 'entity rows' tables that are mapped to objects in code by EF and EF Core.

    This pattern of isolation of entity-specific database tables to utility-generic database tables is the essence and the point of the hierarchical table structure. Without a basic explication of that Functionality, no real communication on this topic will occur.

    But all parties should do the basic research necessary to be able to explain what they are trying to achieve in this discussion and why they may be trying to conflate a TPT implementation pattern and a Data Storage design pattern.

  25. roji commented on May 16, 2021

    @roji
    Member

    @kimballjohnson TPT support was already introduced as an EF Core feature into version 5.0, so the discussion may be moot at this point.

    Regardless, the PostgreSQL hierarchical table arrangement is, well, PostgreSQL-specific. One could argue that it should exist in MySQL and SQL Server, but that's quite out of EF Core's scope. Note that PostgreSQL hierarchical tables are also considered legacy by some.

    So, concretely speaking, users needing inheritance mapping today - on non-PostgreSQL databases - need to make a choice between TPH and TPT; and TPT is known to have problematic perf in some scenarios because of the additional JOINs it introduces(see more about this in our docs).

  26. kimballjohnson commented on May 18, 2021

    @kimballjohnson
  27. added theissue type on Jun 15, 2025
  28. marked Add support for TPT. #14907 as a duplicate of this issue on Jun 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions