84

For Asp.net Core apps, which one do we have to use? AddDbContext or AddDbContextPool? According to EF Core documentation, AddDbContextPool provides high performance but the default Asp.net Core project templates use AddDbContext.

3
  • 12
    Unless you have needs for high performance, you won't need AddDbContextPool. High-performance means 2000-5000 requests (per machine you are running your apps on) per second. 100 requests / min is no high performance and you are good with the defaults
    – Tseng
    Jan 25, 2018 at 14:32
  • 1
    @Tseng How about practically? - @Gabriel's answer mentions having to create new DbContext for parallel queries - will AddDbContextPool be simpler since it does this automatically? Any downsides?
    – Jeppe
    Aug 8, 2019 at 8:32
  • @Jeppe I'm a little late, but pooling won't help you at all with parallel queries. Whether you use pooling or not, you're still only given one DbContext per scope from dependency injection. Aug 19, 2020 at 9:51

1 Answer 1

122

The answer is here (under "DbContext pooling"): https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#dbcontext-pooling

DbContext is not thread-safe. So you cannot reuse the same DbContext object for multiple queries at the same time (weird things happen). The usual solution for this has been to just create a new DbContext object each time you need one. That's what AddDbContext does.

However, there is nothing wrong with reusing a DbContext object after a previous query has already completed. That's what AddDbContextPool does. It keeps multiple DbContext objects alive and gives you an unused one rather than creating a new one each time.

Which one you use is up to you. Both will work. Pooling has some performance gains. However the documentation warns that if you use any private properties in your DbContext class that should not be shared between queries, then you should not use it. I imagine that's pretty rare though, so pooling should be appropriate in most cases.

11
  • 3
    But, and what about sql transations? if transations scope is per connection, in the pool of connection is could be affected?
    – Alexsandro
    Feb 7, 2019 at 17:52
  • The default transaction behavior is that it creates a transaction for each call to SaveChanges() and closes it automatically. So it only might be an issue if you create a transaction manually and don't close it off. Feb 7, 2019 at 19:17
  • 4
    Will returned DbContext keep their underlying connection alive whilst being pooled? I can't seem to find any clue in the docs. Apr 30, 2019 at 21:26
  • 1
    @djsoteric If you are handling the resetting of that property, then yes, you will be fine as long as you are resetting it properly when needed. Feb 18, 2020 at 17:53
  • 13
    Internals: When a DbContext is returned to the context pool, the state of the context is being reset by calling its IResettableService.ResetState() method. This method in turn calls the GetResettableServices() method and then calls IResettableService.ResetState() on the returned objects as well. One of those objects is the EF Core providers RelationalConnection derived object, because it implements IResettableService as well. The default implementation of ResetState() in RelationalConnection is to call its own Dispose() method, which in turn calls DbConnection.Dispose().
    – lauxjpn
    Apr 16, 2020 at 12:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.