Interview frequency: …
Short answer
The same DbContext instance cannot be used by two concurrent operations at once; AddDbContextPool reuses previously created context instances to reduce repeated construction costs.
Explanation
DbContext holds connection state and a Change Tracker that is not protected by concurrency locks. Running Task.WhenAll(db.Users.ToListAsync(), db.Orders.ToListAsync()) on one instance causes EF Core to throw an error. AddDbContextPool keeps a pool of checked and cleaned DbContext instances and recycles them across HTTP requests, noticeably improving requests per second.
Code example
// Enable pooling DbContext
builder.Services.AddDbContextPool<AppDbContext>(options =>
options.UseSqlServer(connectionString));Common mistakes
- Sharing one DbContext across concurrent threads or registering it as a Singleton.