Skip to content

SplitQuery order expression not equality #35144

@Kation

Description

@Kation

Include your code

Having two entities

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; }
}

Use multiple key

modelBuilder.Entity<EntityA>().HasKey(t => new { t.KeyA, t.KeyB });

Query A include B

dataContext.A.Include(t => t.B).OrderBy(t => t.Name).Skip(2).Take(2).ToList();

Split into two sql

SELECT `a`.`KeyA`, `a`.`KeyB`, `a`.`Name`, `a`.`Time`
      FROM `A` AS `a`
      ORDER BY `a`.`Name`,`a`.`KeyA`, `a`.`KeyB`
      LIMIT 2 OFFSET 2

SELECT `b`.`Id`, `b`.`KeyA`, `b`.`KeyB`, `t`.`KeyA`, `t`.`KeyB`
      FROM (
          SELECT `a`.`KeyA`, `a`.`KeyB`
          FROM `A` AS `a`
          ORDER BY `a`.`Name`
          LIMIT 2 OFFSET 2
      ) AS `t`
      INNER JOIN `B` AS `b` ON (`t`.`KeyA` = `b`.`KeyA`) AND (`t`.`KeyB` = `b`.`KeyB`)
      ORDER BY `t`.`KeyA`, `t`.`KeyB

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

Metadata

Metadata

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions