efcore icon indicating copy to clipboard operation
efcore copied to clipboard

AOT .Skip(n).Take(n) query errors at runtime

Open digital88 opened this issue 9 months ago • 0 comments

Bug description

Hello.

When AOT publishing efcore app with query like this:

var page = 10;
var size = 20;
ctx
.Set<MyEntity>()
.OrderBy(e => e.Id)
.Where(e => e.Id > 10)
.Skip(size)
.Take(page)
.ToListAsync(CancellationToken.None);

Publish succeeds, but app crashes in runtime with error:

Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: __p_0
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey, TValue, InsertionBehavior) + 0x300
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) + 0x10

Your code

using Microsoft.EntityFrameworkCore;
#pragma warning disable CA1050,S3903,IL2026,IL3050

public static class Program
{
    public static async Task Main()
    {
        var page = 10;
        var size = 20;
        async Task local()
        {
            using var ctx = new MyContext();
            _ = await ctx
                .Set<MyEntity>()
                .OrderBy(e => e.Id)
                .Where(e => e.Id > 10)
                .Skip(size)
                .Take(page)
                .ToListAsync(CancellationToken.None);
        }
        await local();
    }
}

public class MyEntity
{
    public int Id { get; set; }
}

public class MyContext : DbContext
{
    public MyContext() : base() { }
    public MyContext(DbContextOptions<MyContext> opts) : base(opts) { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("DataSource=empty");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyEntity>(e => e.HasKey(p => p.Id));
    }
}



<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net9.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishAot>true</PublishAot
<InterceptorsNamespaces>$(InterceptorsPreviewNamespaces);Microsoft.EntityFrameworkCore.GeneratedInterceptors</InterceptorsNamespaces>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tasks" Version="9.0.4">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.13.0" />
    <PackageReference Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.13.0" />
  </ItemGroup>

</Project>

Stack traces

Unhandled exception. System.ArgumentException: An item with the same key has already been added. Key: __p_0
   at System.Collections.Generic.Dictionary`2.TryInsert(TKey, TValue, InsertionBehavior) + 0x300
   at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) + 0x10
   at Microsoft.EntityFrameworkCore.GeneratedInterceptors.<Program_EFInterceptors_MyContext_g>F69CBDDA04B8946844EBA4FC5E69DD37AD83FC4DDD5F6DB935355932DC32BF140__EntityFrameworkCoreInterceptors.Query1_Take4[TSource](IQueryable`1, Int32) + 0x68
   at Program.<>c__DisplayClass0_0.<<Main>g__local|0>d.MoveNext() + 0x39c
--- End of stack trace from previous location ---
   at Program.<Main>d__0.MoveNext() + 0x54
--- End of stack trace from previous location ---
   at Program.<Main>() + 0x2c

Verbose output


EF Core version

9.0.4

Database provider

Microsoft.EntityFrameworkCore.Sqlite (also true for PostgreSql)

Target framework

net9.0

Operating system

macOS Ventura

IDE

VSCode

digital88 avatar Apr 18 '25 19:04 digital88