Entity Framework Core's extension method SingleAsync
throws an exception, if more than one element exists. The message of this exception is vague and does not match with it's synchronous LINQ counterpart Single
.
For comparison this is the message for the exception that is thrown by the Single
method.
System.InvalidOperationException: Sequence contains more than one matching element.
And here the exception message thrown by SingleAsync
, which doesn't really tell us what the actual problem is:
System.InvalidOperationException: 'Enumerator failed to MoveNextAsync.'
Expectation:
The exception messages should be equal for both the synchronous and the asynchronous version.
@smitpatel Is this easy to change?
Easy to change. CoreStrings + copy messages from BCL.
https://github.com/aspnet/EntityFrameworkCore/blob/0279e2274677e3ded9e9a3d16db75961f09486ae/src/EFCore/Query/ShapedQueryCompilingExpressionVisitor.cs#L109-L151
@ajcvickers The message returned is correct in ef core 2.2. This is only happening in ef core 3.0. The entire stack strace is changed. May be this should be considered a bug rather than enhancement.
@pavinan Exception messages and stack traces are both non-stable from release to release. (Indeed, stack traces may not be stable from built to build.) Applications should not rely on these things staying the same.
The same happens with FirstAsync
, when no element is returned.
Already done for SingleAsync/SingleOrDefaultAsync in #19092, but I guess FirstAsync and friends remain...
@roji - FirstAsync is covered by SingleAsync already.
Thanks @smitpatel, so I guess this can be closed as fixed by #19092?
Already done for SingleAsync/SingleOrDefaultAsync in #19092, but I guess FirstAsync and friends remain...
@roji, do you mean that you are __hiding__ now the use case when there are multiple matches?
There were two cases, you know, in EF4.x and even in EF Core-Snyhcronuos (see method "Single"), when the exception "InvalidOperationException" was thrown:
With your change, you are always generating the same text for both cases.
That will give us no possibility to distinguish between the above, in order to analyse the problems.
Is it possible in EF Core variant of "SingleAsync" to distinguish between those two cases?
@stefanluchian #19092 corrected the error message for SingleAsync to output these two error messages, depending on whether there are no elements or multiple. Are you seeing an issue somewhere?
I've read only the conversation on #19092, not the entire changes. So I didn't notice the other change about the "multiple" case. I need to improve my skills on github. Please accept my apologies.
One last question, though: which versions of EFCore receive these improvements?
No worries @stefanluchian. This change went in for 5.0.0-preview1, so you can either try out the latest preview, or wait for 5.0.0 to be released.
@smitpatel @ajcvickers anything else to do here beyond what was already merged in #19092?
Most helpful comment
@ajcvickers The message returned is correct in ef core 2.2. This is only happening in ef core 3.0. The entire stack strace is changed. May be this should be considered a bug rather than enhancement.