-
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
Unexpected behavior of value-converted expressions in queries #35515
Comments
The limitations documentation used to contain a relevant bullet point:
but it has been removed in dotnet/EntityFramework.Docs@1a9ef8a (maybe because no warning is logged anymore 🤔 ) |
Hey @ranma42 👋 Sorry it took so long to respond, we're in a particularly intensive work period (which for me will likely continue at least until May - expect delays unfortunately...). Yeah, value converters are indeed incompatible with many (probably most) querying patterns that involve them. Interestingly, I think we see less people struggling with this than I'd expect - people seem to kind of get it, or maybe aren't attempting to involve value-converted properties in non-trivial queries (by which I mean, going beyond comparison with a constant/parameter). I can see us implemented some targeted warnings: your case above of comparing two columns with different value converters seems like a good candidate; though that may end up warning for legitimate scenarios as well (I'd have to think if I can come up with some) - that's also OK, as we have warnings for high-probability (but not 100%) issues as well. I wouldn't see this as high priority simply because again, I don't remember anyone bringing up this paritcular kind of problem. I absolutely agree that our docs here can and should be extended, to make sure people are fully aware of the limitations; basically something like "if you go beyond the basics of comparing to constant/parameter, be very careful and confirm, and be aware that most cases won't work as expected". The two last cases - projecting with arithmetic operators - look like we may want to treat them as low-priority bugs. Just like regular method/member translations, I believe we should consider not allowing operators in these cases. However, this is made a bit more complex by the important distinction between value converters which change store types, and those which don't. In other words, a value converter that just e.g. appends a suffix on a string or divides a number by two is harmless; it shouldn't interfere with any translations in general. Properties which are value-converted to another store type, on the other hand, should be blocked from participating in most translations (operators, methods, members...). There's obviously a lot to think about and discuss here. I'd start by improving the docs; given that we don't see people failing here too much, i wouldn't prioritize the rest too highly right away - but we can of course open issues to track various possibilities. Let me know what you think. |
Bug description
Using value converters seems to be very error prone, as they can be freely used within LINQ expressions, but in several cases the resulting SQL query does not take value conversion into account.
Apparently (under the assumption that the value conversion is a pure function and that the two directions are indeed inverse), equality comparison between a value-converted column against a constant or parameter matches the "obvious" behavior (the one you would get through client-side evaluation).
Most other expressions seem to be prone to mismatching translations, which can lead to exceptions and/or silently return "wrong" results.
This is likely related to
IIUC the plan was to add a warning (see the comment #11347 (comment)) but I was unable to trigger it when running the code and I did not it in the documentation.
If there is still a desire to work towards emitting warnings in these cases, this code example might be a good starting point for further testing/bug hunting (I did not find a PR or an issue with this specific goal).
NOTE: I am not sure whether the code I have written is valid or not 😅
I might be misunderstanding the explanation of the limitations of value converters. The closest one I see is "It isn't possible to query into value-converted properties, e.g. reference members on the value-converted .NET type in your LINQ queries.", but it seems to be aimed specifically at member access, while the issue I am hitting involves simple scalar values (or, rather, expressions on simple scalar values).
If this is not tackled as a bug in EFCore, I think it might be a good idea to extend the documentation to explicitly mention the pitfalls around queries that rely on value-converted fields.
To reproduce, the Sqlite version of the code runs on https://dotnetfiddle.net/ just fine; the SqlServer version needs a DB to connect to.
Your code
Stack traces
Verbose output
EF Core version
9.0.1
Database provider
Microsoft.EntityFrameworkCore.Sqlite
Target framework
.NET 9.0
Operating system
Kali Linux
IDE
Visual Studio Code 1.96.4
EDIT: added one of the relevant issues
The text was updated successfully, but these errors were encountered: