-
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
EF-SQLite: Translation of method 'System.Math.Abs' failed. #28461
Comments
@springy76 the SQLite provider doesn't translate the overload of Math.Abs which accepts a decimal; in general, using a value converter doesn't automatically allow you to use functions defined on the destination type. This is what is meant by this note on the limitations of SQLite:
Consider using double directly in your entity type instead of having a value converter from decimal to double. |
@roji Thats why the conversion to float is in place: It allows sorting: both and comparison, too:
executes WHERE COALESCE("d"."TotalNet", 0.0) > COALESCE("d"."TotalGross", 0.0)
ORDER BY COALESCE("d"."TotalGross", 0.0) DESC Those also work using a custom converter which stores integer cents by applying factor 100 and derives from WHERE COALESCE("d"."TotalNetCents", 0) > COALESCE("d"."TotalGrossCents", 0)
ORDER BY COALESCE("d"."TotalGrossCents", 0) DESC and it executes without errors and the results are valid. EFcore is more capable as you try to tell me ;) |
@springy76 yes, ordering is supported, since the SQL simply places the column name in the ORDER BY clause. Function translation is different: EF needs to recognize the Math.Abs CLR function, which it doesn't currently do. |
Covered by #10434 |
I have an entity
Document
with a decimal property:and ModelBuilder applies conversion to double (as suggested by docs about limits of SQLite):
prop.HasConversion<double>()
Executing this query:
dbcontext.Documents.Where(d => Math.Abs(d.TotalNet ?? 0) > Math.Abs(d.TotalGross ?? 0) ).ToList();
fails with
InvalidOperationException
:before any SQL got created. (BTW: The link with number 2132413 is totally unhelpful and total nonsense).
ExceptionCallstack: HResult -2146233079
In comparison the same using SQL-Server and
prop.HasColumnType("money")
produces+executes SQL:and works.
In comparison the same using PostgreSQL (npgsql) and
prop.HasColumnType("money")
produces+executes SQL:and fails with
PostgresException
:Include provider and version information
EF Core version: 6.0.7
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: .NET 6.0
Operating system: Win10
IDE: VS 2022 / LINQPad
The text was updated successfully, but these errors were encountered: