-
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
SplitQuery order expression not equality #35144
Comments
I don't understand what the error is. Here's a minimal skeleton. Can you adjust it to make it fail? using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using var db = new MyContext();
db.Database.OpenConnection();
db.Database.EnsureCreated();
db.A.Include(t => t.B).Skip(2).Take(2).ToList();
public class MyContext : DbContext
{
public DbSet<EntityA> A { get; set; }
public DbSet<EntityB> B { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<EntityA>().HasKey(t => new { t.KeyA, t.KeyB });
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.LogTo(Console.WriteLine, events: [RelationalEventId.CommandExecuting]);
optionsBuilder.UseSqlite(o => o.UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery));
}
}
public class EntityA
{
public string KeyB { get; set; }
public string KeyA { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
public ICollection<EntityB> B { get; set; }
}
public class EntityB
{
public string KeyA { get; set; }
public string KeyB { get; set; }
public int Id { get; set; }
[ForeignKey("KeyA,KeyB")]
public EntityA A { get; set; }
} |
@cincuranet
get difference results with
In some case. |
@cincuranet
Return
Return
|
The expectation here doesn't seem right to me; your LINQ query is the following: var list = dataContext.A
.Include(t => t.B)
.OrderByDescending(t => t.Name)
.Skip(4)
.Take(4)
.ToList(); You're only requesting the results to by ordered by By the way, I removed split querying, and saw the same behavior with single querying (your program reports "should 1: 0)". The SQL generated is: SELECT [a0].[KeyA], [a0].[KeyB], [a0].[Name], [b].[Id], [b].[KeyA], [b].[KeyB]
FROM (
SELECT [a].[KeyA], [a].[KeyB], [a].[Name]
FROM [A] AS [a]
ORDER BY [a].[Name] DESC
OFFSET @__p_0 ROWS FETCH NEXT @__p_0 ROWS ONLY
) AS [a0]
LEFT JOIN [B] AS [b] ON [a0].[KeyA] = [b].[KeyA] AND [a0].[KeyB] = [b].[KeyB]
ORDER BY [a0].[Name] DESC, [a0].[KeyA], [a0].[KeyB] Note that the inner subquery does not have ordering on |
@roji The point is that split querying generate query A sql add primary keys to
But when it generate query B sql that select from
In MySql return difference
So
|
Apologies, I looked again and you're right. This issue has already been fixed in the latest bits (so EF Core 10) - unfortunately it make it in time to be released for EF 9.0; the tracking issue is #26808. I confirmed that before that fix (#34097), the ORDER BYs are missing from the subquery and the program outputs "Should be 1:0", but after that fix, the ORDER BYs are present and the program outputs "Should be 1:1". |
Duplicate of #26808 |
Include your code
Having two entities
Use multiple key
Query A include B
Split into two sql
First sql will generate order by fieds which primary key not used in order by.
But second sql will not generate it.
In some situation that results is different.
ICollection<EntityB> B
will not have correct result.I'm not sure it is ef core or provider issue.
EF Core version: 8.0.11
Database provider: Pomelo.EntityFrameworkCore.MySql 8.0.2
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.12
The text was updated successfully, but these errors were encountered: