-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Support inheritence with JSON POCO mapping #27779
Comments
related: #9630 |
Copied from my original issue Support in EF7 and upcoming work in EF8 for JSON is coming along nicely, and I was wondering if there might be room to continue expanding on it. This specific idea is to support of interfaces or otherwise abstract types in JSON columns, through the use of a type hint that gets embedded with the data. I've accomplished this in the past by writing my own An example of how this could be useful is for storing arrays/collections as part of a known JSON type: {
"configurations": [
{
"_discriminator": "configuration-type-1",
"specificToAll": "All types would have this.",
"specificToOne": "Only type one would have this."
},
{
"_discriminator": "configuration-type-2",
"specificToAll": "All types would have this.",
"specificToTwo": "Only type two would have this."
}
],
"singleConfiguration": {
"_discriminator": "configuration-type-3",
"specificToAll": "All types would have this.",
"specificToThree": "Only type three would have this."
}
} This could be deserialized into something like: public class ApplicationSettings
{
public IList<Configuration> Configurations { get; set; } = new();
public Configuration SingleConfiguration { get; set; }
}
public interface Configuration
{
public string SpecificToAll { get; }
}
public class ConfigurationTypeOne : Configuration
{
public string SpecificToAll { get; set; }
public string SpecificToOne { get; set; }
}
public class ConfigurationTypeTwo : Configuration
{
public string SpecificToAll { get; set; }
public string SpecificToTwo { get; set; }
}
public class ConfigurationTypeThree : Configuration
{
public string SpecificToAll { get; set; }
public string SpecificToThree { get; set; }
} New EF-based JSON filtering would only allow filtering on the fields it has reason to expect, so in this case, |
This would be extremely useful. Currently anything other than simple "complex" structures aren't supported by EF Owned Types (either multi-column-mapped or JSON-column-mapped), limiting their use and having to fall back to string columns and managing our own JSON. |
Note for team: should we have a separate issue for this in Cosmos? |
@ajcvickers I think so... Though we need to more clearly agree on what this means - at this point I think it refers to TPH-style inheritance for complex types. |
I've done this myself using the technique mentioned above of having a custom converter. One major issue I ran into is that System.Text.Json requires that the first property be the |
This limitation has already been removed for .NET 9.0 (issue). |
Note #28592, which is also about mapping multiple types to the same JSON document (or sub-document), but where the types aren't in a hierarchy - also via the use of a discriminator ( |
I need this, although with my model the discriminator not only needs to have a custom name (not |
Yeah this is what I did. Super simple |
@chrisc-ona -- https://gist.github.com/atrauzzi/dde4f5e92fb783cb6847ff2b1c2d6710 Hopefully this is of some use. I'll repeat the usual disclaimer when I share it: It works, it seems to work well, but I'm sure there are optimizations or deeper integrations with EF that can be done to make it much better overall. |
@hahn-kev @atrauzzi thanks. Unfortunately I'm not sure how well either of these approaches would work for my particular use case though since where polymorphism applies in our model is not at the column/top level document level, but a few levels deep. It looks like at least for now I'll have to continue treating the column as a string and explicitly convert to/from JSON. |
@roji Is this planned for EF 10? Is there a process to request that it is? |
@shravan2x you upvote the issues you want implemented and then the team can use that when they prioritize the issues/feature requests. |
System.Text.Json has polymorphic deserialization in 7.0. We should implement a similar scheme here with
$type
, so that JSON documents we produce are compatible with that.Originally requested in npgsql/efcore.pg#2321
The text was updated successfully, but these errors were encountered: