EntityFrameworkServiceCollectionExtensions.AddPooledDbContextFactory Method

Definition

Overloads

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

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type where instances are pooled for reuse.

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

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type where instances are pooled for reuse.

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

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type where instances are pooled for reuse.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<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 AddPooledDbContextFactory<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddPooledDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddPooledDbContextFactory(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 DbContext to be created by the factory.

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

Registering a factory instead of registering the context type directly allows for easy creation of new DbContext instances. Registering a factory is recommended for Blazor applications and other situations where the dependency injection scope is not aligned with the context lifetime.

Use this method when using dependency injection in your application, such as with Blazor. 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, Using DbContext factories, and Using DbContext pooling for more information and examples.

Applies to

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

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type where instances are pooled for reuse.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<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 AddPooledDbContextFactory<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddPooledDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddPooledDbContextFactory(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 DbContext to be created by the factory.

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

Registering a factory instead of registering the context type directly allows for easy creation of new DbContext instances. Registering a factory is recommended for Blazor applications and other situations where the dependency injection scope is not aligned with the context lifetime.

Use this method when using dependency injection in your application, such as with Blazor. 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, Using DbContext factories, and Using DbContext pooling for more information and examples.

Applies to