Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discriminator support for many values to shared types #15365

Closed
rdadkins opened this issue Apr 15, 2019 · 3 comments
Closed

Discriminator support for many values to shared types #15365

rdadkins opened this issue Apr 15, 2019 · 3 comments

Comments

@rdadkins
Copy link

rdadkins commented Apr 15, 2019

I am trying to achieve a TPH approach with discriminators where there are multiple values that share the same type. I have a class structure as follows:

public class BaseLineItem
{
    public int Id { get; set; }

    public string LineItemType { get; set; } // Non-null discriminator

    // Other shared properties
}

public class LineItemType1 : BaseLineItem
{
    // Specific logic
}

public class LineItemType2 : BaseLineItem
{
    // Specific logic
}

And my configuration looks like this:

// BaseLineItem configuration
buidler.HasDiscriminator(e => e.LineItemType)
  .HasValue("DEFAULT")
  .HasValue<LineItemType1>("TYPE1")
  .HasValue<LineItemType2>("TYPE2")
  .HasValue<LineItemType1>("OTHER_TYPE_SIMILAR_TO_1")
  .HasValue<LineItemType2>("OTHER_TYPE_SIMILAR_TO_2");

The problem I am facing is that previous values specified with the discriminator gets wiped away and the last specified value types will be used instead. I expect that a line item with type == "TYPE1" OR type == "OTHER_TYPE_SIMILAR_TO_1" should both map to LineItemType1. Instead, I am restricted to LineItemType1 == "OTHER_TYPE_SIMILAR_TO_1" and LineItemType2 == "OTHER_TYPE_SIMILAR_TO_2". This can be seen in the query generated by EF Core:

SELECT *
FROM [LINE_ITEMS] AS [li]
WHERE [li].[lineitem_type] IN (N'DEFAULT', N'OTHER_TYPE_SIMILAR_TO_1', N'OTHER_TYPE_SIMILAR_TO_2')

Further technical details

EF Core version: 2.2.3
Database Provider: SqlServer (2.2.3)
Operating system: Windows 10 (1809)
IDE: Jetbrains Rider (2019.1 EAP 6)

@ajcvickers
Copy link
Member

Note for triage. Related to #10140 and #4722.

@rdadkins
Copy link
Author

From #10140:

Would an explicit list of discriminator values, instead of just one, work for you instead of the wildcard? In other words, you could say that values "A", "B", and "C" will all result in creation of the same type "X".

Perhaps something like the following similar to this:

buidler.HasDiscriminator(e => e.LineItemType)
  .HasValue("DEFAULT")
  .HasValue<LineItemType1>("TYPE1", "OTHER_TYPE_SIMILAR_TO_2")
  .HasValue<LineItemType2>("TYPE2", "OTHER_TYPE_SIMILAR_TO_2");

In such a case, when encountering type "X", which discriminator value would you expect EF to choose when none is set?

buidler.HasDiscriminator(e => e.LineItemType)
  .HasDefaultValue<BaseLineItem>("DEFAULT");

Or use the base type (given it is not abstract). Or throw an exception during configuration if a default value type is not specified.

@ajcvickers
Copy link
Member

Closing as duplicate of #10140

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants