@@ -5,11 +5,15 @@ namespace Benchmarks.Model;
55
66public sealed class DatabaseContext : DbContext
77{
8- private static readonly Lazy < DbContextOptions < DatabaseContext > > Options = new ( CreateOptions , LazyThreadSafetyMode . ExecutionAndPublication ) ;
8+ private static readonly Lazy < DbContextOptions < DatabaseContext > > TrackingOptions = new ( ( ) => CreateOptions ( true ) , LazyThreadSafetyMode . ExecutionAndPublication ) ;
99
10- public static DatabaseContext Create ( ) => new ( Options . Value ) ;
10+ private static readonly Lazy < DbContextOptions < DatabaseContext > > NoTrackingOptions = new ( ( ) => CreateOptions ( false ) , LazyThreadSafetyMode . ExecutionAndPublication ) ;
1111
12- private static DbContextOptions < DatabaseContext > CreateOptions ( )
12+ public static DatabaseContext CreateTracking ( ) => new ( TrackingOptions . Value , true ) ;
13+
14+ public static DatabaseContext CreateNoTracking ( ) => new ( NoTrackingOptions . Value , false ) ;
15+
16+ private static DbContextOptions < DatabaseContext > CreateOptions ( bool tracking )
1317 {
1418 var services = new ServiceCollection ( ) ;
1519
@@ -21,16 +25,20 @@ private static DbContextOptions<DatabaseContext> CreateOptions()
2125
2226 builder . UseInternalServiceProvider ( provider )
2327 . UseNpgsql ( "Server=tfb-database;Database=hello_world;User Id=benchmarkdbuser;Password=benchmarkdbpass;SSL Mode=Disable;Maximum Pool Size=512;NoResetOnClose=true;Enlist=false;Max Auto Prepare=4;Multiplexing=true" )
24- . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking )
2528 . EnableThreadSafetyChecks ( false )
2629 . UseModel ( DatabaseContextModel . Instance ) ;
2730
31+ if ( ! tracking )
32+ {
33+ builder . UseQueryTrackingBehavior ( QueryTrackingBehavior . NoTracking ) ;
34+ }
35+
2836 return builder . Options ;
2937 }
3038
31- internal DatabaseContext ( DbContextOptions < DatabaseContext > options ) : base ( options )
39+ internal DatabaseContext ( DbContextOptions < DatabaseContext > options , bool tracking = false ) : base ( options )
3240 {
33- ChangeTracker . AutoDetectChangesEnabled = false ;
41+ ChangeTracker . AutoDetectChangesEnabled = tracking ;
3442 }
3543
3644 public DbSet < World > World { get ; set ; }
0 commit comments