-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Description
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
3ranga