-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Enhance InvalidOperationException message fo better cause understaning #24452
Comments
The very last queryable operator in that whole expression is throwing error. We print out whole expression so that you can correlated it with your query. |
Ok it's clear and does make sense. Can you confirm that the descriptive part of the exception message (the one that states that the query "could not be translated") is a fixed message and doesn't contains specific indications? In the example I've made, the problem appears just adding an OrderBy (for pagination pourpose) on a column of the select. It is strange that this can leads to query compilation error. I am inclined to think that there may be some limitation with EF in case of complex query, but of course I may be completely wrong. Do you have any suggestion on how to face those kind of problems, starting from the information contained into InvalidOperationException? |
@lucacestola - Can you share the example query? Words are not really great at communicating LINQ query especially the one which is causing strange errors. |
I hope that the dump of the exception is enough because in code the query is composed by more steps.
The same query without the last OrderBy is translated correctly. |
|
I agree that analyzing complex patterns is costly in terms of effort and also that the developer needs to understand how things work now compared to older versions of EF. I just started converting old project to .net5 so it's easy to be lazy and avoid to study a lot before getting hands on code 😄. With your indications it was easy to understand that the cause was an addition of a datetime with a timespan. Converting it with an DateTime.AddDays fixed the client eval. At this point I'm trying to figure out how to better face those kind of situation and even if experience and knowledge is the best tool, I think that having some more advice, could help developer to better learn. I see that it's not longer possible to use ConfigureWarnings to configure exception on client evaluation and I guess that this is due to the rethink of client eval itself that is now focused on avoid client eval on potential performance leaks situations, that now always lead to an exception. And I'm happy with that! Anyway, precisely because of the complex patterns, it would be good if the developer could state (on query basis?) when client eval is not expected and so avoid unwanted situations. I know that having specific method for debug purpose may be an unwanted API design, but I hope this discussion may help addressing some initial difficulties at the best. |
Good discovery on DateTime + TimeSpan. I did not figure out myself exact cause of client eval. This sort of client eval - in projection - was allowed in EF6 also. It is in all versions of EF Core too. Since it is expected to perform client eval in projection (because at times you are just creating DTO objects which is also client eval in one way), we never warned for those. Even if you had enabled exception on client eval in previous version of EF Core, such Select would not give error. Essentially in previous version it would throw error in same way. So I am not sure, if there is any good way to allow users to know the Select did client eval which doesn't cause unnecessary warnings/errors. May be a debug method as you suggested, we can look into it but most likely it would throw for almost every select which is not single entity being projected out. For the particular issue of DateTime + TimeSpan, efcore/src/EFCore.SqlServer/Query/Internal/SqlServerSqlTranslatingExpressionVisitor.cs Lines 91 to 98 in e6d926f
We should add detailed errors. Though it won't help this case since the client eval is in Projection and error throwing place is different. |
While writing some complex queries it's easy to face an InvalidOperationException that says that the expression can't not be translated.
Often I find an obvious cause just by taking a better look at a misuse of an untranslatable function or a bad use of a group by. Sometimes, however, it is a little tougher and I start breaking the query into several steps until I find the point where it becomes untranslatable.
This time I ran into a strange situation where applying an OrderBy breaks the query translation and however I tried to simplify or make the query more straightforward, I was unable to get to work. The InvalidOperationException states that the whole Expression cannot be translated, but I guess the engine knows on which part of the expression it fails transalating.
It would be helpful if the exception could suggest which part of the expression caused the problem and, of course, why it couldn't. The exception should be more specific than a System.InvalidOperationException, with at least the specific Expression (or a list of them) where the problem occurred, but it would be better to also have the reason.
The text was updated successfully, but these errors were encountered: