DBContext pooling ASP.NET Core
To achieve high performance in application we should use DbContext pooling. Here instead of creating object each and every time we can use DbContext pooling and when new instance is next requested, that pooled instance is returned instead of setting up a new one. Context pooling allows you to pay context setup costs only once at program startup, rather than continuously.
To enable context pooling, simply replace AddDbContext with AddDbContextPool:
// For smaller applications builder.Services.AddDbContextPool<ApplicationDbContext>( options => options.UseSqlServer(connectionString), poolSize: 128 ); // For large applications builder.Services.AddDbContextPool<ApplicationDbContext>( options => options.UseSqlServer(connectionString), poolSize: 2048 );
The poolSize parameter of AddDbContextPool sets the maximum number of instances retained by the pool (defaults to 1024). Once poolSize is exceeded, new context instances are not cached and EF falls back to the non-pooling behavior of creating instances on demand.