Returning `SqlFragmentExpression` from `HasTranslation` doesn't work (switches to client evaluation)
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
I have the following function mapping with `HasTranslation`:
```csharp
public static partial class EntityFrameworkExtensions
{
public static int TotalRowCount()
=> throw new NotSupportedException();
public static void HasTotalRowCountFunction(this ModelBuilder mb)
{
mb
.HasDbFunction(() => TotalRowCount())
.HasTranslation(arguments =>
{
return new SqlFragmentExpression("COUNT(*) OVER()");
});
}
}
```
But when I use the `TotalRowCount` function in a projection, the `SqlFragmentExpression` is ignored and the `TotalRowCount` function is evaluated client-side (the `NotSupportedException` is thrown). When I return a `SqlFunctionExpression` instead, the correct SQL is generated and the SQL function is correctly called.
I am observing a similar behavior when using an `IMethodCallTranslator´ to do the same translation. But in this case another strange behavior can be observed: If my function has a parameter, the translator is called, if it has no parameters (provided by LINQ query), like the method above, the translator isn't even called.
I did look into the EF source code quite a bit but I am feeling kind of lost. Could you please give me a tip why this doesn't work, how I could possibly achieve this simple mapping or at least some hints where I could look in the source code.
I really appreciate any help. Thank you.
Contributor guide
Assessment
This issue has not been assessed yet.