-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
EFCore translates query with GROUP BY and JOIN into invalid SQL #12304
Comments
Duplicate of #10012 |
@smitpatel to document workaround. Fix is considered too risky at this time for patch release. |
Work-around: var allowedIds = new int[] { 1827, 7186 }; //static array of int ids
var query = (from rp in (from c in db.Calculations
select new { Key = c.Period })
.Distinct()
join x in db.Calculations on rp.Key equals x.Period
where allowedIds.Contains(x.Id) && !x.IsOfficial && x.ExpirationDate == null
orderby rp.Key
select x);
//error happens here
var calcs = query.ToList(); Generates SQL SELECT [t].[Key], [x].[Id], [x].[ExpirationDate], [x].[IsOfficial], [x].[Period]
FROM (
SELECT DISTINCT [c].[Period] AS [Key]
FROM [Calculations] AS [c]
) AS [t]
INNER JOIN [Calculations] AS [x] ON [t].[Key] = [x].[Period]
WHERE ([x].[Id] IN (1827, 7186) AND ([x].[IsOfficial] = 0)) AND [x].[ExpirationDate] IS NULL
ORDER BY [t].[Key] The error happens because grouping is not being referenced apart from key selector so our blocking mechanism in #10012 fails and we translate group by with join generating invalid SQL. |
Duplicate of #12826 |
Closing as #12826 tracks adding regression test for this. |
EFCore translates LINQ query into invalid T-SQL code.
Exception message:
Msg 156, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'AS'.
Steps to reproduce
The EFCore query produces the following T-SQL code:
The SQL is invalid.
Further technical details
EF Core version: 2.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: 15.7.3 / 15.8.0 PREVIEW 2
The text was updated successfully, but these errors were encountered: