dotnet / dotnet/efcore

Add testing for Find/FindAsync with default QueryTrackingBehavior

Open
#32,762 3 comments 0 reactions 0 assignees View on GitHub
area-query area-test customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

Find doesn't respect the QueryTrackingBehavior.

This test fails even though QueryTrackingBehavior is set to NoTrackingWithIdentityResolution (NoTracking also fails)
```cs

public QueryTrackingTests()
{
var options = new DbContextOptionsBuilder()
.UseSqlite($"Filename={_dbname}")
.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTrackingWithIdentityResolution)
.Options;
_context = new PrimaryContext(options);
}

[Fact]
public void FindReturnsAsNoTracking()
{
var record = _context.TestItems.Find(_recordKey);
var reloaded = _context.TestItems.Find(_recordKey);
Assert.NotSame(record, reloaded);
}
```

Full test code [here](https://github.com/AceCoderLaura/EfCoreBugs/blob/f2b669ac54f77fdf87f2e67d59252c3913345617/QueryTrackingTests.cs#L41).

The documentation for Find says "If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database." But I think this should not apply when QueryTrackingBehavior is set to NoTracking. NoTracking should mean no tracking, but I'm receiving a tracked record.

### Include provider and version information

EF Core version:
Database provider: Microsoft.EntityFrameworkCore.Sqlite
Target framework: net8.0
Operating system: Windows 10 Pro
IDE: Rider 2023.3.2

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.