-
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
Using new[] in select query gives exception .NET 8 #32331
Labels
area-query
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
regression
Servicing-approved
type-bug
Milestone
Comments
Minimal repro query: _ = context.Blogs
.Select(b => new[] { b.Updated, b.Posts.Max(p => p.Updated) }.Max())
.ToList(); Full repro source codeawait using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
_ = context.Blogs
.Select(b => new[] { b.Updated, b.Posts.Max(p => p.Updated) }.Max())
.ToList();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Blog
{
public int Id { get; set; }
public DateTime Updated { get; set; }
public DateTime Updated2 { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public DateTime Updated { get; set; }
public Blog Blog { get; set; }
} In 7.0 this indeed translated, with the outer Max() evaluated client-side: SELECT [b].[Updated], (
SELECT MAX([p].[Updated])
FROM [Post] AS [p]
WHERE [b].[Id] = [p].[BlogId])
FROM [Blogs] AS [b] This only fails when a subquery is inside the inline collection. Given the following LINQ query: _ = context.Blogs
.Select(b => new[] { b.Updated, b.Updated2 }.Max())
.ToList(); ... 8.0 produces the following, translating the outer Max() to SQL: SELECT (
SELECT MAX([v].[Value])
FROM (VALUES ([b].[Updated]), ([b].[Updated2])) AS [v]([Value]))
FROM [Blogs] AS [b] ... whereas 7.0 again evaluates client-side: SELECT [b].[Updated], [b].[Updated2]
FROM [Blogs] AS [b] Opened #32332 to improve the 8.0 translation to use |
roji
added a commit
to roji/efcore
that referenced
this issue
Nov 30, 2023
roji
added
the
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
label
Nov 30, 2023
roji
added a commit
to roji/efcore
that referenced
this issue
Nov 30, 2023
roji
added a commit
to roji/efcore
that referenced
this issue
Nov 30, 2023
roji
added a commit
that referenced
this issue
Nov 30, 2023
Reopening to consider servicing. |
roji
added a commit
to roji/efcore
that referenced
this issue
Nov 30, 2023
Fixes dotnet#32331 (cherry picked from commit 50a8ae5)
roji
added a commit
to roji/efcore
that referenced
this issue
Nov 30, 2023
Fixes dotnet#32331 (cherry picked from commit 50a8ae5)
roji
added a commit
to roji/efcore
that referenced
this issue
Jan 3, 2024
Fixes dotnet#32331 (cherry picked from commit 50a8ae5)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area-query
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
customer-reported
regression
Servicing-approved
type-bug
File a bug
Include your code
I could not find anything about this and therefore decided to post this.
This code worked with .NET 7 and Microsoft.EntityFrameworkCore.SqlServer Version 7.0.13:
Updated = p.ThreatAndCountermeasures.Count() > 0 ? new[] { p.Updated, p.ThreatAndCountermeasures.Max(tac => tac.Updated) }.Max() : p.Updated,
With .NET 8 and Microsoft.EntityFrameworkCore.SqlServer Version 8.0.0 I get the following exception:
If I use this code it works:
Updated = p.ThreatAndCountermeasures.Count() > 0 ? new List<DateTime> { p.Updated, p.ThreatAndCountermeasures.Max(tac => tac.Updated) }.Max() : p.Updated,
https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-8.0/breaking-changes
Include provider and version information
EF Core version: 8.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 8.0
Operating system: Windows 11 Pro OS build 22621.2506
IDE: Microsoft Visual Studio 2022 (64-bit) - Current Version 17.8.0
The text was updated successfully, but these errors were encountered: