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
Convert.ToInt32 could not be translated when argument is user-defined function #30818
Comments
I created a minimal project that reproduces the problem: https://github.com/LaXiS96/EFCoreIssue30818 |
Note for triage: very similar to #28287. |
I did some more testing and updated my repro repository. This is the
and this is for the dictionary access (before my interceptor):
In the interceptor, this is the returned expression for the new string property:
and this is for the translated dictionary access:
The only difference is in the property access expression that is passed as the first argument to |
Note from triage: we should also translate the |
Hello,
I am refactoring/rewriting an application which has been using EFCore since around 2.0 and stores
Dictionary<string, object?>
entity properties as JSON string columns in SQL Server.The original implementation abused internal EFCore classes to rewrite dictionary accesses to
JSON_VALUE
SQL functions, among other things. I have now switched to EFCore 7.0, using value converters and interceptors to try and achieve the same (and more) functionality, but I'm getting stuck somewhere.I cannot use the recently introduced JSON columns support because my dictionaries are dynamic and cannot be mapped to a static model.
I have a
JsonDictionary
class which extendsDictionary<string, object?>
and implements equality interfaces and overrides, plus value converter and comparer for JSON serialization and change tracking. CRUD operations on theDbContext
are working as expected.My entity looks like this:
I need to filter values within the Dictionary property in SQL, which is why I configured this user defined function for
JSON_VALUE
:I then have an
IQueryExpressionInterceptor
which replaces calls to dictionary indexers andget_Item
with calls toJsonValue
defined above.For example,
entity.Dictionary["key"]
is translated toJsonValue(entity.Dictionary, $"$.\"key\"")
within the LINQ expression tree.Here is the issue I am having, this works as expected:
while this doesn't:
The Expression DebugView after my interceptor in the former case is:
while for the latter it is:
and this is the exception I get in this case:
I was expecting
Convert.ToInt32(entity.Dictionary["key"]) == value
to be translated toCONVERT(int, JSON_VALUE([Dictionary], '$."key"')) = @value
or something along those lines.Why does EFCore complain about not being able to translate
Convert.ToInt32
calls (which should be supported by the SQL Server provider according to documentation)?Is there something wrong with my database function setup?
Thank you for any insight you might have
Include provider and version information
EF Core version: 7.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer 7.0.5
Target framework: net6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.5.5
The text was updated successfully, but these errors were encountered: