You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consider EF support (if any) for the upcoming C# discriminated unions #36375
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.
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:
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
interfaceIAnimalIdentity{publicGuidId{get;init;}}classCatEntity:IAnimalIdentity{publicGuildId{get;init;}publicintClawCount{get;set;}};
classBirdEntity:IAnimalIdentity{publicGuidId{get;init;}publicintClawCount{get;set;}}// With maybe an attribute for the Identity resolution for non fluent approach?// [IdentityInterface(typeof(IAnimalIdentity)]unionAnimalEntity(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.
Activity
AndriySvyryd commentedon Feb 4, 2026
This is closely related to #757 and #7623
And somewhat related to #10789 and #10140
Kuurama commentedon Jun 8, 2026
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.
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.
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 typesThen 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.