-
Notifications
You must be signed in to change notification settings - Fork 161
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
Collection querying via EF7 JSON column #816
Comments
Could you show how a working query would look like if made directly against the EF dbcontext? |
@ravz OData doesn't know/use the type behind the scene. OData builds the model/type using the C# classes. for example, I have the following C# entity (School). public class School
{
public int SchoolId { get; set; }
// It's not for Edm model, let's ignore when Edm model building
public string Emails { get; set; }
public IList<string> ContactEmails
{
get
{
return Emails is null ? new List<string>() : JsonSerializer.Deserialize<IList<string>>(Emails);
}
set
{
Emails = value is null ? string.Empty : JsonSerializer.Serialize(value);
}
}
} where:
I think this is the same scenario as yours. In this case, you have to convert the Linq expression built from OData. I have an example for your reference at: https://github.com/xuzhg/mydotnetconf/blob/main/OData/OData.WebApi/Extensions/SchoolStudentFilterBinder.cs#L12 Or see my commit at: xuzhg/mydotnetconf@508d0a3 to make |
@ravz @julealgon I created a new issue for EFCore at: dotnet/efcore#30132. That's the JSON array column. |
hi @xuzhg my example is a bit more complicated since my JSON columns map to objects
I want to be able to run a query like can it be done? |
@ravz Yes, you can. Here's my sample for your reference: https://github.com/xuzhg/mydotnetconf/blob/main/README.md#updated-on-272023-make-the-basic-query-on-json-array-column Here's the commit: xuzhg/mydotnetconf@26e01ca Be noted, I only implemented the basic scenario with 'Tricks'. Let me know any other problems. |
Not sure if this is just not supported yet or I'm doing something wrong
I have a List of Addresses stored in database as a JSON array
[{"StreetAddress":"1615 Anzac Avenue","City":"Kallangur","State":"QLD","Postcode":"4503","Latitude":-27.24795613,"Longitude":153.01077159},{"StreetAddress":"1611 Anzac Avenue","City":"Kallangur","State":"QLD","Postcode":"4503","Latitude":-27.24779426,"Longitude":153.01015158}]
I've mapped this using the new Entity Framework 7 JSON column support
When I query it via OData the results return as expected
However when I try to use any filter on the collection I get LINQ expression errors:
e.g. $filter=Addresses/$count gt 0
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The LINQ expression 'DbSet<ApplicationODataRecord>() .Where(a => EF.Property<List<Address>>(a, "Addresses") .AsQueryable() .LongCount() > __TypedProperty_0)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
e.g. $filter=Addresses/any(a: a/Postcode eq '4503')
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: The LINQ expression 'DbSet<ApplicationODataRecord>() .Where(a => EF.Property<List<Address>>(a, "Addresses") .AsQueryable() .Any(o => (string)o.Postcode == __TypedProperty_0))' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
The text was updated successfully, but these errors were encountered: