Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

QueryTrackingBehavior resets to TrackAll when using DbContext pooling #29733

Closed
JudeVajira opened this issue Dec 2, 2022 · 2 comments · Fixed by #29766
Closed

QueryTrackingBehavior resets to TrackAll when using DbContext pooling #29733

JudeVajira opened this issue Dec 2, 2022 · 2 comments · Fixed by #29766
Labels
area-dbcontext closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Milestone

Comments

@JudeVajira
Copy link

When using DbContext pooling while using NoTracking as the default QueryTrackingBehavior, it resets back to TrackAll incorrectly second time a DbContext is provided through the DI. Works in EF Core 6.0.11

using Microsoft.EntityFrameworkCore;

var host = Host.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddDbContextPool<BlogDbContext>(opt =>
        {
            opt.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
            opt.UseInMemoryDatabase("blog");
        });
        services.AddLogging(conf => conf.SetMinimumLevel(LogLevel.Error));
    })
    .Build();

QueryTrackingBehavior trackingOne, trackingTwo;

await using (var scope = host.Services.CreateAsyncScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<BlogDbContext>();

    trackingOne = dbContext.ChangeTracker.QueryTrackingBehavior;
}

await using (var scope = host.Services.CreateAsyncScope())
{
    var dbContext = scope.ServiceProvider.GetRequiredService<BlogDbContext>();

    trackingTwo = dbContext.ChangeTracker.QueryTrackingBehavior;
}

if (trackingOne == trackingTwo)
{
    Console.WriteLine($"Tracking resets properly in EFCore 6: {trackingOne}");
}
else
{
    Console.WriteLine($"TrackAll in EFCore 7. [trackingOne: {trackingOne}, trackingTwo: {trackingTwo}]");
}

public class BlogDbContext : DbContext
{
    public BlogDbContext(DbContextOptions<BlogDbContext> options)
        : base(options)
    {
    }
}

Output EF Core 6.0.11

Tracking resets properly in EFCore 6: NoTracking

C:\Users\JudeVajira\source\repos\EfCorePooledTracking\EfCorePooledTracking\bin\Debug\net6.0\EfCorePooledTracking.exe (process 34224) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Output EF Core 7.0.0

TrackAll in EFCore 7. [trackingOne: NoTracking, trackingTwo: TrackAll]

C:\Users\JudeVajira\source\repos\EfCorePooledTracking\EfCorePooledTracking\bin\Debug\net7.0\EfCorePooledTracking.exe (process 4620) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .

Include provider and version information

EF Core version: 7.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer, Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET 7.0
Operating system: Windows 10 (21H2)
IDE: Visual Studio 2022 17.4.0

@JudeVajira
Copy link
Author

JudeVajira commented Dec 2, 2022

Runnable repo but you will need to switch the TFM to run on EFCore 6.

@roji
Copy link
Member

roji commented Dec 2, 2022

Confirmed, thanks for the minimal repro.

@ajcvickers ajcvickers self-assigned this Dec 2, 2022
@ajcvickers ajcvickers added this to the 7.0.x milestone Dec 4, 2022
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Dec 4, 2022
@ajcvickers ajcvickers reopened this Dec 8, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.x, 7.0.3 Dec 8, 2022
@ajcvickers ajcvickers removed their assignment Aug 31, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area-dbcontext closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported regression Servicing-approved type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants