Skip to content

Json POCO mapping and type hierarchy #2321

Closed

Description

@forik

Hello, I have a table entity like this:

class Entity
{
    [Column(TypeName = "jsonb")]
    public BaseType Config {get;set;}
}

And a number of types derived from BaseType:

public class Derived1 : BaseType { public string StringProp {get;set;} }

public class Derived2 : BaseType { public int IntProp {get;set;} }

... etc you got the idea.

Is this possible to achieve?

I tried to configure a custom ValueConverter<BaseType, JsonDocument> and conversion to JsonDoument was easy.
Conversion from JsonDocument can be tricky as JsonDocument doesn't contain a ContainsKey method or similar but doable.

Missing part is querying as Where(x => ((Dervived)x.Config).IntProp == 1) couldn't be translated.

Activity

  1. roji commented on Apr 7, 2022

    @roji
    Member

    This currently isn't supported out of the box, since it would require some strategy of storing the type inside the JSON document in some way (i.e. is it a Derived1 or a Derived2?). This is typically done by introducing some sort of discriminator property (like EF Core already does for Type-Per-Hierarchy mapping, see docs), but nothing like this is supported for JSON at the moment.

    Some serious work on JSON is planned for EF Core 7.0 - I'll make sure we have this on our radar, though it definitely isn't a guarantee this would be supported for that version.

  2. roji commented on Apr 7, 2022

    @roji
    Member

    Opened dotnet/efcore#27779 to track this on the EF Core side, closing here.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions