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

Cast JSON POCO to Derived Type? #2710

Closed
TimFeldmann-Work opened this issue Apr 1, 2023 · 3 comments
Closed

Cast JSON POCO to Derived Type? #2710

TimFeldmann-Work opened this issue Apr 1, 2023 · 3 comments

Comments

@TimFeldmann-Work
Copy link

I'm not entirely sure if this is feasible, but I wanted to ask anyway just in case I was missing something obvious.

I am building a MassTransit-like package for my work. I want to give developers who use my package the ability to query a JsonDocument instance like they would with LINQ instead of relying on methods such as JsonDocument.RootElement.GetProperty("x")... for cases where Saga data needs to be manually manipulated.

I am currently relying on the JsonDocument type but wanted to see if I could make this work with the POCO mapping approach.

Here is a sample of what I am trying to achieve:

public class JsonDerivedEntity : JsonBaseEntity
{
    public int IntTest { get; set; } = 5;
}
public class BaseEntity
{
    public Guid Id { get; set; }
    [Column(TypeName = "jsonb")]
    public JsonBaseEntity JsonEntity { get; set; }

    public BaseEntity()
    {
        Id = Guid.NewGuid();
    }
}
var baseEntity = new BaseEntity();

baseEntity.JsonEntity = new JsonDerivedEntity() 
{ 
    IntTest = 11 
};

dbContext.JsonBaseEntity.Add(baseEntity);

await dbContext.SaveChangesAsync();

dbContext.ChangeTracker.Clear();

var result = await dbContext.JsonBaseEntity
    .FirstOrDefaultAsync(x => ((JsonDerivedEntity)x.JsonEntity).IntTest == 11);

The insertion portion of this code works as expected, the issue I'm having is I can't figure out if it's possible to cast the JsonBaseEntity to a derived type. My plan was to create a generic method which would cast JsonEntity to the generic type provided by the user. This would allow developers to query the JSON entity with JSON-supported LINQ as is provided by example here.

My issue is with specifying a base type on the entity, and trying to cast that type to the actual type the dev is wanting to query for. When attempting to cast the entity to a derived type, I receive the following exception:

The LINQ expression could not be translated.

Thanks for any help and suggestions on this!

@shravan2x
Copy link

@roji Perhaps the framework could check for the presence of the JsonPolymorphic attribute, and implicitly translate the query above to WHERE JsonEntity->>'type' == '<JsonDerivedEntity's_Type_from_JsonDerivedAttribute>' AND JsonEntity->>'IntTest' == 11?

@roji
Copy link
Member

roji commented Oct 10, 2024

Duplicate of dotnet/efcore#27779

@roji roji marked this as a duplicate of dotnet/efcore#27779 Oct 10, 2024
@roji
Copy link
Member

roji commented Oct 10, 2024

This is definitely on the roadmap on the EF side (at which point it'll work also on PG), but is not implemented at this point (there's a good chance it will get done for 10).

@roji roji closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2024
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

3 participants