-
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 not generating SQL query correctly on fields that have conversion #31817
Comments
What kind of SQL are you expecting for c.FechaCompra.Date? Given that c.FechaCompra is a number in the database, extracting a date out of that doesn't work (at least not without some serious hacking). More generally, calling methods/members on value converted properties is not supported, see #10434. You can use user-defined function mapping to have some custom static .NET function which, when used in a query, translates to arbitrary SQL which would perform the date extraction; but you'd still need to figure out how to extract a date from your database long in T-SQL. |
Duplicate of #10434 |
First of all, thank you for the quick response. Secondly, following on the topic, the SQL I'm expecting is the same but just removing the "CONVERT" statement, so it would simply compare a BIGINT to a BIGINT: SELECT COUNT(*)
FROM [APP_TICKETS_OPERACIONES] AS [a]
WHERE (CASE
WHEN [a].[FechaCompra] >= CAST(20230917000000 AS bigint) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END & CASE
WHEN [a].[FechaCompra] <= CAST(20230917000000 AS bigint) THEN CAST(1 AS bit)
ELSE CAST(0 AS bit)
END) = CAST(1 AS bit) I see that EF core is able to correctly translate to SQL the conversion from DateTime to long and viceversa, so the issue is not that it has trouble converting properties. The problem is that it converts both the date to a long and the long to a date, and later comparing them, which ends up causing the exception of "Explicit conversion from bigint to date data type is not allowed." I will dig deeper into #10434 and the resources you attached because, for now, I still don't get why it can't be done. |
@HU373 I'm not quite following... Your LINQ query has |
Setup
I have an entity model with a DateTime property named "Fecha Compra":
This DateTime value is stored in the SQLServer database as a BIGINT with the format "yyyyMMddHHmmss". Let's say I have the date 2023/09/20 09:15:00, then the BIGINT stored in the database is 20230920091500. This is achieved using the ".HasConversion" method in the property:
Issue
My problem comes when I try to filter using this field. For example, I have the follwing chunk of code:
When entity automatically converts this to the SQL query I get the following:
As you can see, it is trying to compare a date on the left side of the comparison (it even has to convert it!!!) to a bigint in the right side of the comparison. So this gets me an exception every time:
Provider and version information
EF Core version: 7.0.11
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Windows 11 22H2
IDE: Visual Studio 2022 17.7.4
The text was updated successfully, but these errors were encountered: