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

Json POCO mapping and type hierarchy #2321

Closed
forik opened this issue Apr 6, 2022 · 2 comments
Closed

Json POCO mapping and type hierarchy #2321

forik opened this issue Apr 6, 2022 · 2 comments

Comments

@forik
Copy link

forik commented Apr 6, 2022

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.

@roji
Copy link
Member

roji commented Apr 7, 2022

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.

@roji
Copy link
Member

roji commented Apr 7, 2022

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

@roji roji closed this as completed Apr 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants