Skip to content

Consider EF support (if any) for the upcoming C# discriminated unions #36375

Description

@roji

dotnet/csharplang#8928

For example, we could map them using a TPH-like strategy, with a discriminator.

Activity

  1. added this to the 11.0.0 milestone on Jul 15, 2025
  2. added theissue type on Jul 15, 2025
  3. AndriySvyryd commented on Feb 4, 2026

    @AndriySvyryd
    Member

    This is closely related to #757 and #7623
    And somewhat related to #10789 and #10140

  4. modified the milestones: 11.0.0, Backlog on Feb 4, 2026
  5. Kuurama commented on Jun 8, 2026

    @Kuurama

    Hi.
    I couldn't find any discussion relating to design or decision regarding this topic.

    Since the preview of C# 15 are appearing to be more and more capable, I wanted to give my personal takes on design regarding EFCore support. (For TPH mapping, my thought are unrelated to TPT).

    For now, I see two different way to design a domain using unions, each with their pros and cons.

    • In the first design, and to address basic mapping needs without relying on deep underlying tracking, union can just be a complex type for the data an entity is holding.
    record CatData(int ClawCount);
    record BirdData(int MaxFlyingHeight);
    
    union AnimalData(CatData, BirdData);
    class AnimalEntity
    {
        public Guid Id { get; init; }
        public AnimalData Data { get; set; }
    }

    Here, the union would only be used as a complex type, where it would be mapped as a discriminated union given a type discriminator mapping in the AnimalEntity table.

    One very cool thing for state management is that it allows changing the underlying 'imaginary' runtime type from a Cat to a Bird, given the Animal takes a potion to change all his data, all without having to create another entity because only the data it's holding changes type.

    This sadly also causes tracking to write the whole complex type each time things update, but the design is quite consistent nonetheless.

    • As the second design, it would be just like our current polymorphic type storing system:
    class CatEntity
    { 
        public Guild Id { get; init; }
        public int ClawCount { get; set; }
    };
    class BirdEntity
    {
        public Guid Id { get; init; }
        public int ClawCount { get; set; }
    }
    
    union AnimalEntity(CatEntity, BirdEntity);

    However, how would that translate to EFCore mapping?
    The AnimalEntity type doesn't hold any data per say, there is no way to map and identify invariants in the identity of each cases.

    The Cat and Bird must share the same identity, therefore, how would that be possible? I don't think It would work without some trickery (Runtime reflection and crash on missmatch?, ew?). Maybe I'm wrong though, but here is my idea:

    UnionTypes with Identity resolution must implement their identity as a common interface between case types

    interface IAnimalIdentity
    {
        public Guid Id { get; init; }
    }
    
    class CatEntity : IAnimalIdentity
    { 
        public Guild Id { get; init; }
        public int ClawCount { get; set; }
    };
    class BirdEntity : IAnimalIdentity
    {
        public Guid Id { get; init; }
        public int ClawCount { get; set; }
    }
    
    // With maybe an attribute for the Identity resolution for non fluent approach?
    // [IdentityInterface(typeof(IAnimalIdentity)]
    union AnimalEntity(CatEntity, BirdEntity);

    Then the underlying mapping (for fluent mapping) would provide a method to bind the common Identity Interface between each types.

    That was my thought on the design, however, with the second approach, we loose the way to mutate a Cat to a Bird, and instead we can't rely all the way on tracking because we might need to change the runtime type sometime, therefore, creating a new instance and drop using tracking entirely.

    If you take a more Bank/Transactional design model, developpers might rely more on type mutation than field mutation.
    Maybe both those approach are great but for different purpose only?

    What do you guys think of it? I personally like the first approach better because that puts clear boudary on Entity with Identity vs the type of data it holds for a TPH hierarchy. You loose the fine grained mutation with tracking, but hey, can't have everything sometime.

    Perhaps the solution is just to rely on closing the hierarchy and drop using unions for it. It would be kinda be fair in a way.

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