dotnet / dotnet/efcore

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

Open
#35,968 0 comments 0 reactions 0 assignees View on GitHub
area-precompiled-queries area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

### Bug description

Hello.

When AOT publishing efcore app with query like this:
```cs
var page = 10;
var size = 20;
ctx
.Set()
.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

```csharp
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()
.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 opts) : base(opts) { }

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

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


Exe
net9.0
enable
enable
true$(InterceptorsPreviewNamespaces);Microsoft.EntityFrameworkCore.GeneratedInterceptors





all
runtime; build; native; contentfiles; analyzers; buildtransitive



```

### Stack traces

```text
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.F69CBDDA04B8946844EBA4FC5E69DD37AD83FC4DDD5F6DB935355932DC32BF140__EntityFrameworkCoreInterceptors.Query1_Take4[TSource](IQueryable`1, Int32) + 0x68
at Program.<>c__DisplayClass0_0.<g__local|0>d.MoveNext() + 0x39c
--- End of stack trace from previous location ---
at Program.d__0.MoveNext() + 0x54
--- End of stack trace from previous location ---
at Program.() + 0x2c
```

### Verbose output

```text

```

### 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.