-
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
After upgrading from EF 6.0.1 to EF 6.0.2: broken query generation when using aggregate #27621
Comments
Just noted that it also fails on EF Core 6.0.2. Will update issue |
Reviewing changes in EF 6.0.2, I suspect the issue #26592 could be related |
This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate. |
I created a repro repository here: https://github.com/farlop/EFCore_Issue27621 Just running the project will launch the issue. Downgrading to EF 6.0.1 will run the query correctly. Please let me know if the repro is good enough, or if you need more work on it. |
@farlop alternatively, as workaround you can break the query into two parts - fetch the relevant OrderRequirement ids in first query (using Any, which is the problematic part when combined with group by) and then perform grouping based on the ids in the second one, like so: var gas = db.GrowerAssignmentsView(null, null, null, null).Where(x => x.GrowerCode == "GRW");
var orIds = await db.OrderRequirements
.Where(x => gas.Any(y => y.CustomerCode == x.Customer.Code && y.Year == x.Year && y.WeekNumber == x.WeekNumber && y.FamilyCode == x.FamilyCode))
.Select(x => x.Id)
.ToListAsync();
var workaround = await db.OrderRequirements
.Where(x => orIds.Contains(x.Id))
.GroupBy(x => new { CustomerCode = x.Customer.Code, CustomerDescription = x.Customer.Description, x.Year, x.WeekNumber, x.FamilyCode },
(x, gr) => new { x.CustomerCode, x.CustomerDescription, x.Year, x.WeekNumber, x.FamilyCode, MaxArrivalDate = gr.SelectMany(o => o.OrderRequirementDetails.Select(d => d.ArrivalDate)).Max() })
.ToListAsync(); |
dupe of #27433 |
Thank you @maumar for the workarounds. For now we will stick with version 6.0.1 or will apply the recommended Switch, as the query is far more complex that what's in the repro. |
Having some code that generates this query in EF 6.0.1:
After upgrading to EF 6.0.2, the query generated is broken when trying to get
MAX([t0].[ArrivalDate]) AS [MaxArrivalDate]
, adding a new subquery that also is broken and fails as wrong table identifiers (f3) is used in every [fGrowerAssignments] use:This is failing with message:
Here is more information about Model:
EF Core version: 6.0.2
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.2 Preview 1
The text was updated successfully, but these errors were encountered: