Skip to content

Discriminator support for many values to shared types #15365

Closed as duplicate

Description

@rdadkins

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)

Activity

  1. ajcvickers commented on Apr 15, 2019

    @ajcvickers
    Contributor

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

  2. rdadkins commented on Apr 15, 2019

    @rdadkins
    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.

  3. ajcvickers commented on Apr 15, 2019

    @ajcvickers
    Contributor

    Closing as duplicate of #10140

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions