-
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
Cross apply-like query produces invalid SQL #24061
Comments
If you:
Using raw sql command is always the best fallback. For this issue, I guess the ef core team will re-design the area around If you don't want to hardcode your sql text in your C# code or you need to switch between different database providers, you can create a database view or stored procedure. To map raw sql to any .NET type rather than predefined entity types, see #22742 |
Thank you for suggestion, @anranruye. But that is not my case. We have too much entities in our database (1000+) and trying to avoid creating new ones without real need (such as temp tables, update queries and so on). Project startup time grows non-linearly with the growth of context entities count. And of course it's realy hard to support raw SQL in project. I want to believe that EF Core 6.0 will bring back complex queries I was able to create by means of EF6. |
That's inaccurate - EF Core 5 supports many GroupBy patterns, but some patterns are indeed unsupported. Improving this is part of the EF Core 6 plan (we aim to get as close to query feature parity with the non-core EF). Some more information on what's supported can be found in the complex query operators docs. @smitpatel will probably comment on the specific issue. |
@Akridian Unfortunately, ef core 6 will be released almost end of the year. How about using another orm for those linq queries which are not supported by ef core?(However, I can't give advise for the choice of the orm tool) |
@anranruye don't worry. My current workaround is not the best one but takes appropriate time/resources for our tasks for now. If data size grows we will consider usage of stored procedure. |
I believe that the core issue for invalid SQL is fixed in EF Core 5.0. We stopped adding additional columns used for correlation in Select for Group by because it is invalid. Though it may run into issues of finding a way to correlated outer & inner. Since you want CROSS APPLY in your query, consider using SelectMany operator rather than just Select to be very specific and EF 5.0 may be able to handle that. We will need your model classes to write a working query with desired SQL. |
@smitpatel I use such syntax because it is sql-like and provided as example in doc. |
Duplicate of #22892 |
Bug
I was trying to implement the folowing query in our service:
Here is the code:
And this query produces invalid SQL:
Which failed with exception:
Join with ROW_NUMBER hint does not work with GROUP BY. Expected CROSS APPLY query. Possibly related to this issue.
Workaround
As workaround I tried to use subquery:
This produce valid SQL:
But subquery is presented in query twice and it has very high calculation cost.
Expected reusage of scalar value like:
I end up with next code:
Which uses following SQL:
And filters data on client side. That leads to useless DB-Service traffic.
Provider and version information
EF Core version: 3.1.4
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.1
Operating system: Windows 10
IDE: Microsoft Visual Studio Enterprise 2019 16.8.4
The text was updated successfully, but these errors were encountered: