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.

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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

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

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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.

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

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

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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.

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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.

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

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

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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;
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

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.

Returns

IServiceCollection

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

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.

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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.

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;
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

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.

Returns

IServiceCollection

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

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.

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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;
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

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.

Returns

IServiceCollection

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

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.

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.

For more information on how to use this method, see the Entity Framework Core documentation at https://aka.ms/efdocs. For more information on using dependency injection, see https://go.microsoft.com/fwlink/?LinkId=526890.

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.

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;
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

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.

Returns

IServiceCollection

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

Applies to