Multiple discriminators for same type

Viewed 179

I got the following types

internal enum IssueType
{
    First,
    Second,
    Special
}

internal class Issue
{
    public int       Id    { get; set; }
    public string    Title { get; set; }
    public IssueType Type  { get; set; }
}

internal class SpecialIssue : Issue
{
    public string Payload { get; set; }
}

Some issue types map to certain subclasses, like Special would map to SpecialIssue in this case. Others just map to Issue i.e. there are several IssueTypes without special subclasses.

Now I would like to have an Issues table to hold all these issues so I configured Issue.Type as a discriminator value.

builder.HasDiscriminator(x => x.Type)
       .HasValue<Issue>(IssueType.First)
       .HasValue<Issue>(IssueType.Second)
       .HasValue<SpecialIssue>(IssueType.Special);

Unfortunately it seems that I cannot set multiple discriminator values for the same type so at runtime I get errors stating that some discriminators weren't mapped although I did.

What I would like to achieve is to map all subclasses to their respective discriminators and have some kind of "fallback" that maps to Issue (or just map any remaining IssueType to the Issue type manually).

Is it possible to achieve this? I couldn't figure out any way to get EFCore to do this.
I'm currently using EFCore 5.0.10.

0 Answers
Related