Entity Framework Core service default lifetime

前端 未结 3 400
生来不讨喜
生来不讨喜 2020-11-27 12:53

In ASP.NET Core application I can register DbContext through DI like this

services.AddDbContext(options => options.UseNpgsql(         


        
3条回答
  •  情话喂你
    2020-11-27 13:18

    Note: In EF Core 2 there is now a new method AddDbContextPool which creates a pool of contexts that can be reused. The scoping is still the same, but the instance will be 'reset' and returned to the pool. I would have thought the overhead of 'resetting' would be the same as just creating a new one but I guess that isn't the case.

    If this method is used, at the time a DbContext instance is requested by a controller we will first check if there is an instance available in the pool. Once the request processing finalizes, any state on the instance is reset and the instance is itself returned to the pool.+

    This is conceptually similar to how connection pooling operates in ADO.NET providers and has the advantage of saving some of the cost of initialization of DbContext instance.

    https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#high-performance

提交回复
热议问题