Closed virzak closed 2 years ago
Hi, the reason was something else.
EF is not adding the type mappings to conversions (like (long)e.Count
) if the target type (long
) has a "default mapping".
/* From RelationalSqlTranslatingExpressionVisitor.cs */
// Introduce explicit cast only if the target type is mapped else we need to client eval
if (unaryExpression.Type == typeof(object)
|| Dependencies.TypeMappingSource.FindMapping(unaryExpression.Type, Dependencies.Model) != null)
{
sqlOperand = _sqlExpressionFactory.ApplyDefaultTypeMapping(sqlOperand);
return _sqlExpressionFactory.Convert(sqlOperand!, unaryExpression.Type, typeMapping: null); // <-- the root cause
}
Now, I check whether the conversion has a type mapping and if not then I add it explicitly.
The version 4.0.0-beta06
is on the way.
Thanks for the prompt response and the commit. Would you say that this is a bug in EF Core?
I can't say. Probably EF is handling this case later during the query translation.
Filed this behaviour at dotnet/efcore#27075
The workaround only good if the conversion happens at the top level expression. Here is another failing test case.
.ThenBy((long)e.ConvertibleClass + 1)
Doesn't seem like it should be on the plugin developer to recursively adjust type mappings.
Yes, less work for me :)
Everything should be working now.
Here is a failing test case, which should pass
Looks like it is caused by the null mappings in the following line:
The exception is thrown here:
https://github.com/dotnet/efcore/blob/70cd92974e09e938a92384e91b5c3179f4773725/src/EFCore.Relational/Query/QuerySqlGenerator.cs#L681