Skip to content

Support Polymorphic Relationships #7623

Open

Description

@bjorn-ali-goransson

For example: I have two Entity Types Page and Post, and both implement the interface ITeaser. Let's say that ITeaser has a Title.

Now, I want my Post to have a new property RecommendedNextRead that is of type ITeaser, and can thus be of both type Post and Page.

What would be the best way to implement this? Is it even feasible supported?

Activity

  1. changed the title [-]Reference to Type spanning across several entity types (and therefore tables)[/-] [+]Support Polymorphic Relationships[/+] on Feb 22, 2017
  2. added this to the Backlog milestone on Feb 22, 2017
  3. rowanmiller commented on Feb 22, 2017

    @rowanmiller
    Contributor

    It's not currently supported, unless Post/Tag have a common base type (but that means they would need to be stored in the same table with TPH... presumably not what you want 😄). We have discussed supporting this, but it's not something we're working on at the moment or in the immediate future. Moving to the backlog.

  4. bdebaere commented on May 28, 2019

    @bdebaere

    Copied from my StackOverflow post.

    Consider the following table design...

        Order
          Id UNIQUEIDENTIFIER
        
        OrderLine
          Id UNIQUEIDENTIFIER
          OrderId UNIQUEIDENTIFIER
        
        Connection
          Id UNIQUEIDENTIFIER
          FromEntityId UNIQUEIDENTIFIER
          ToEntityId UNIQUEIDENTIFIER
    

    It allows linking any table to any other table via the Connection table. The alternative being to create a ConnectionFromTxToTy table for every single combination. Which in our schema means creating 70! (! for factorial) tables just for this functionality. On top of this that would mean adding 70 navigation properties to every class. This is far from easy to implement or consume.

    It is possible to add a navigation property Connections to my models. Which does allow me to view all Connections linked to a single entity (provided I double them up).

        order
            .HasMany(entity => entity.Connections)
            .WithOne()
            .HasForeignKey(entity => entity.FromEntityId)

    However, I'd like to go one step further and actually be able to include the linked entity, like any other entity. The downside of course is that this is not supported by EntityFrameworkCore. I cannot create a class like below and expect it to work out of the box.

        public class Connection
        {
            public Guid Id { get; set; }
        
            public Guid FromEntityId { get; set; }
        
            public Guid ToEntityId { get; set; }
        
            public virtual object FromEntity { get; set; }
        
            public virtual object ToEntity { get; set; }
        }

    And as soon I would try to include on Orders.Include(order => order.Connections).ThenInclude(connection => connection.ToEntity) I would lose all typed functionality. But this I could live with if something like the following would function: Orders.Include(order => order.Connections).ThenInclude(connection => connection.ToEntity).ThenInclude(orderLine => ((OrderLine)orderLine).Order).

    Currently I've included the linked entity as JSON, but that obviously means I lose all include functionality after that. But worse than that, any ValueConverters I assigned to that type aren't being executed.

    How can I tackle this challenge? I'm not sure what's the best path: the virtual IDictionary<string, object> route or the virtual object route or something else. I feel like I might potentially get there with a custom QueryModelGenerator and a custom SqlServerQuerySqlGeneratorFactory, although I don't know if that alone would be enough. But EntityFrameworkCore stops me already at the navigation property with type object.

  5. bjorn-ali-goransson commented on May 28, 2019

    @bjorn-ali-goransson
    Author

    Hey,

    Sounds like a relational database isn't the right tool for the job, honestly ...

  6. bricelam commented on Jul 29, 2020

    @bricelam
    Contributor

    See also #757

  7. atrauzzi commented on Oct 31, 2020

    @atrauzzi

    I'd definitely like to see polymorphic relations supported and if the number of times it's asked for even in this project's bug tracker is any indicator... It's quite high priority.

  8. AndriySvyryd commented on Oct 31, 2020

    @AndriySvyryd
    Member

    @atrauzzi This issue currently only has 9 👍 votes, so it's actually rather low on the feature request list

  9. atrauzzi commented on Oct 31, 2020

    @atrauzzi

    @AndriySvyryd - I've been scanning this repository as well as stack overflow (as well as others) and the combined organic demand is a much different story.

    I get you're trying to be objective, but that methodology leans into a disingenuous extreme. It takes empathy to truly understand what users want, not the expectation that people find the right place to put a "thumbs up" as well as to self-select in such a specific way.

    Hopefully feature priority for this project can be more visionary, not reactionary...

  10. AndriySvyryd commented on Oct 31, 2020

    @AndriySvyryd
    Member

    Yes, we take more thing into account for the release planning process, this is just the most accessible metric.

  11. atrauzzi commented on Nov 1, 2020

    @atrauzzi

    Right, but I don't want it to become a bad-faith distraction to my point which is that I think demand for this in particular is more than the most accessible metric.

    My apologies, but also in my defence, I do have a knack for these things.

  12. AndriySvyryd commented on Nov 1, 2020

    @AndriySvyryd
    Member

    We are gathering feedback to make sure that we don't miss any gaps not currently reflected in the issues. We'll adjust our plans if necessary according to the data.

  13. atrauzzi commented on Nov 1, 2020

    @atrauzzi

    Hmm. That's really no different to waiting for +1s. It substitutes analysis & intuition with statistics and intellect. This goes back to my previous point about being visionary and not reactionary.

    I get it, the next thing to say is that if people don't speak up, they can't be helped... But then what is the long term impact of that and what blind spots does it create? Current sentiment towards EF core is hurting .net in general and quite a few long-term asks have been sitting in the freezer since 2014.

    The impression given here is that the data is bad due to a bad methodology and it's pulling focus away from the things people actually want.

  14. roji commented on Nov 1, 2020

    @roji
    Member

    @atrauzzi we aren't just looking at data - analysis and intuition are also telling us that certain other issues are probably more important to our users (the thing about intuition is that it's a bit hard to substantiate 🤣). I also don't believe in purely data- and metric-driven decisions, but I don't think this project does that in general. It's easy to say that a certain feature is important or has been in the backlog for a long time. What's hard is to gauge its importance compared to everything else that users want.

  15. 9 remaining items

  16. SoftCircuits commented on Oct 2, 2023

    @SoftCircuits

    @ajcvickers

    @abdumostafa There shouldn't be any issue mapping those entities now. See my sample code below:

    I didn't understand your sample code. How is the relationship between Employees and Documents mapped in the database? I don't see any foreign keys.

    Also, was anything new added to .NET to better support this since you posted that?

    Thanks.

  17. MoishyS commented on Nov 9, 2023

    @MoishyS

    I am surprised that it's still not in plan, Laravel has had it for like 10 years already.
    https://laravel.com/docs/master/eloquent-relationships#one-to-many-polymorphic-relations

    posts
        id - integer
        title - string
        body - text
     
    videos
        id - integer
        title - string
        url - string
     
    comments
        id - integer
        body - text
        commentable_id - integer
        commentable_type - string
    

    #15854 (comment) would be a workaround but that's also not planned, any workaround for now?

  18. EmperorArthur commented on Feb 13, 2024

    @EmperorArthur

    I'm going to also request this. Especially since #25141 was rolled into it.

    EF Core is supposed to be something usable by large organizations. Yet a consequence of that environment is we often don't get to adjust the DB to what a library expects. DBAs don't like the performance impact of adding a column where every value is the same, and probably see forcing the use of raw SQL and stored procedures as a plus.

  19. da3dsoul commented on Feb 19, 2025

    @da3dsoul

    I have an issue involving this that I managed to work around via a switch clause and TPH (not pretty but worked). This would be very nice to have moved forward. I don't have much to add, but #25141 is what I need to clean it up. Thanks in advance. Hopefully, it happens in this year or next. It's been on the backburner for a long time and has gotten a lot of planning, so who knows.

  20. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions