Share via


EntityFrameworkServiceCollectionExtensions.AddDbContextPool Method

Definition

Overloads

AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

AddDbContextPool<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

AddDbContextPool<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

AddDbContextPool<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 128) where TContext : Microsoft.EntityFrameworkCore.DbContext;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextPool : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContextPool(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddDbContextPool(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 1024) As IServiceCollection

Type Parameters

TContext

The type of context to be registered.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<DbContextOptionsBuilder>

A required action to configure the DbContextOptions for the context. When using context pooling, options configuration must be performed externally; OnConfiguring(DbContextOptionsBuilder) will not be called.

poolSize
Int32

Sets the maximum number of instances retained by the pool. Defaults to 1024.

Returns

The same service collection so that multiple calls can be chained.

Remarks

DbContext pooling can increase performance in high-throughput scenarios by re-using context instances. However, for most application this performance gain is very small. Note that when using pooling, the context configuration cannot change between uses, and scoped services injected into the context will only be resolved once from the initial scope. Only consider using DbContext pooling when performance testing indicates it provides a real boost.

Use this method when using dependency injection in your application, such as with ASP.NET Core. For applications that don't use dependency injection, consider creating DbContext instances directly with its constructor. The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure a connection string and other options.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using DbContext with dependency injection and Using DbContext pooling for more information and examples.

Applies to

AddDbContextPool<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 128) where TContext : Microsoft.EntityFrameworkCore.DbContext;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextPool : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContextPool(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddDbContextPool(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 1024) As IServiceCollection

Type Parameters

TContext

The type of context to be registered.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

A required action to configure the DbContextOptions for the context. When using context pooling, options configuration must be performed externally; OnConfiguring(DbContextOptionsBuilder) will not be called.

poolSize
Int32

Sets the maximum number of instances retained by the pool. Defaults to 1024.

Returns

The same service collection so that multiple calls can be chained.

Remarks

DbContext pooling can increase performance in high-throughput scenarios by re-using context instances. However, for most application this performance gain is very small. Note that when using pooling, the context configuration cannot change between uses, and scoped services injected into the context will only be resolved once from the initial scope. Only consider using DbContext pooling when performance testing indicates it provides a real boost.

Use this method when using dependency injection in your application, such as with ASP.NET Core. For applications that don't use dependency injection, consider creating DbContext instances directly with its constructor. The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure a connection string and other options.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using DbContext with dependency injection and Using DbContext pooling for more information and examples.

This overload has an optionsAction that provides the application's IServiceProvider. This is useful if you want to setup Entity Framework Core to resolve its internal services from the primary application service provider. By default, we recommend using AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

Applies to

AddDbContextPool<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 128) where TContextService : class where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContextService : class where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
static member AddDbContextPool : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'ContextService : null and 'ContextImplementation :> Microsoft.EntityFrameworkCore.DbContext and 'ContextImplementation :> 'ContextService)
<Extension()>
Public Function AddDbContextPool(Of TContextService As Class, TContextImplementation As Class) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddDbContextPool(Of TContextService As Class, TContextImplementation As Class) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 1024) As IServiceCollection

Type Parameters

TContextService

The class or interface that will be used to resolve the context from the container.

TContextImplementation

The concrete implementation type to create.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<DbContextOptionsBuilder>

A required action to configure the DbContextOptions for the context. When using context pooling, options configuration must be performed externally; OnConfiguring(DbContextOptionsBuilder) will not be called.

poolSize
Int32

Sets the maximum number of instances retained by the pool. Defaults to 1024.

Returns

The same service collection so that multiple calls can be chained.

Remarks

DbContext pooling can increase performance in high-throughput scenarios by re-using context instances. However, for most application this performance gain is very small. Note that when using pooling, the context configuration cannot change between uses, and scoped services injected into the context will only be resolved once from the initial scope. Only consider using DbContext pooling when performance testing indicates it provides a real boost.

Use this method when using dependency injection in your application, such as with ASP.NET Core. For applications that don't use dependency injection, consider creating DbContext instances directly with its constructor. The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure a connection string and other options.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using DbContext with dependency injection and Using DbContext pooling for more information and examples.

Applies to

AddDbContextPool<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)

Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 128) where TContextService : class where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextPool<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContextService : class where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
static member AddDbContextPool : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'ContextService : null and 'ContextImplementation :> Microsoft.EntityFrameworkCore.DbContext and 'ContextImplementation :> 'ContextService)
<Extension()>
Public Function AddDbContextPool(Of TContextService As Class, TContextImplementation As Class) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddDbContextPool(Of TContextService As Class, TContextImplementation As Class) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 1024) As IServiceCollection

Type Parameters

TContextService

The class or interface that will be used to resolve the context from the container.

TContextImplementation

The concrete implementation type to create.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

A required action to configure the DbContextOptions for the context. When using context pooling, options configuration must be performed externally; OnConfiguring(DbContextOptionsBuilder) will not be called.

poolSize
Int32

Sets the maximum number of instances retained by the pool. Defaults to 1024.

Returns

The same service collection so that multiple calls can be chained.

Remarks

DbContext pooling can increase performance in high-throughput scenarios by re-using context instances. However, for most application this performance gain is very small. Note that when using pooling, the context configuration cannot change between uses, and scoped services injected into the context will only be resolved once from the initial scope. Only consider using DbContext pooling when performance testing indicates it provides a real boost.

Use this method when using dependency injection in your application, such as with ASP.NET Core. For applications that don't use dependency injection, consider creating DbContext instances directly with its constructor. The OnConfiguring(DbContextOptionsBuilder) method can then be overridden to configure a connection string and other options.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using DbContext with dependency injection and Using DbContext pooling for more information and examples.

This overload has an optionsAction that provides the application's IServiceProvider. This is useful if you want to setup Entity Framework Core to resolve its internal services from the primary application service provider. By default, we recommend using AddDbContextPool<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

Applies to