Share via


EntityFrameworkServiceCollectionExtensions.AddDbContextFactory Method

Definition

Overloads

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

AddDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

AddDbContextFactory<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext and 'Factory :> Microsoft.EntityFrameworkCore.IDbContextFactory<'Context>)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext, TFactory As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

Type Parameters

TContext

The type of DbContext to be created by the factory.

TFactory

The type of IDbContextFactory<TContext> to register.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

An optional action to configure the DbContextOptions for the context. This provides an alternative to performing configuration of the context by overriding the OnConfiguring(DbContextOptionsBuilder) method in your derived context.

If an action is supplied here, the OnConfiguring(DbContextOptionsBuilder) method will still be run if it has been overridden on the derived context. OnConfiguring(DbContextOptionsBuilder) configuration will be applied in addition to configuration performed here.

In order for the options to be passed into your context, you need to expose a constructor on your context that takes DbContextOptions<TContext> and passes it to the base constructor of DbContext.

lifetime
ServiceLifetime

The lifetime with which to register the factory and options. The default is Singleton

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.

For convenience, this method also registers the context type itself as a scoped service. This allows a context instance to be resolved from a dependency injection scope directly or created by the factory, as appropriate.

This overload allows a specific implementation of IDbContextFactory<TContext> to be registered instead of using the default factory shipped with EF Core.

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 AddDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

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 factories for more information and examples.

Applies to

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext and 'Factory :> Microsoft.EntityFrameworkCore.IDbContextFactory<'Context>)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext, TFactory As DbContext) (serviceCollection As IServiceCollection, Optional optionsAction As Action(Of DbContextOptionsBuilder) = Nothing, Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

Type Parameters

TContext

The type of DbContext to be created by the factory.

TFactory

The type of IDbContextFactory<TContext> to register.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<DbContextOptionsBuilder>

An optional action to configure the DbContextOptions for the context. This provides an alternative to performing configuration of the context by overriding the OnConfiguring(DbContextOptionsBuilder) method in your derived context.

If an action is supplied here, the OnConfiguring(DbContextOptionsBuilder) method will still be run if it has been overridden on the derived context. OnConfiguring(DbContextOptionsBuilder) configuration will be applied in addition to configuration performed here.

In order for the options to be passed into your context, you need to expose a constructor on your context that takes DbContextOptions<TContext> and passes it to the base constructor of DbContext.

lifetime
ServiceLifetime

The lifetime with which to register the factory and options. The default is Singleton

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.

For convenience, this method also registers the context type itself as a scoped service. This allows a context instance to be resolved from a dependency injection scope directly or created by the factory, as appropriate.

This overload allows a specific implementation of IDbContextFactory<TContext> to be registered instead of using the default factory shipped with EF Core.

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 factories for more information and examples.

Applies to

AddDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, Optional optionsAction As Action(Of DbContextOptionsBuilder) = Nothing, Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) 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>

An optional action to configure the DbContextOptions for the context. This provides an alternative to performing configuration of the context by overriding the OnConfiguring(DbContextOptionsBuilder) method in your derived context.

If an action is supplied here, the OnConfiguring(DbContextOptionsBuilder) method will still be run if it has been overridden on the derived context. OnConfiguring(DbContextOptionsBuilder) configuration will be applied in addition to configuration performed here.

In order for the options to be passed into your context, you need to expose a constructor on your context that takes DbContextOptions<TContext> and passes it to the base constructor of DbContext.

lifetime
ServiceLifetime

The lifetime with which to register the factory and options. The default is Singleton

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.

For convenience, this method also registers the context type itself as a scoped service. This allows a context instance to be resolved from a dependency injection scope directly or created by the factory, as appropriate.

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 factories for more information and examples.

Applies to

AddDbContextFactory<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

Registers an IDbContextFactory<TContext> in the IServiceCollection to create instances of given DbContext type.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) 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>

An optional action to configure the DbContextOptions for the context. This provides an alternative to performing configuration of the context by overriding the OnConfiguring(DbContextOptionsBuilder) method in your derived context.

If an action is supplied here, the OnConfiguring(DbContextOptionsBuilder) method will still be run if it has been overridden on the derived context. OnConfiguring(DbContextOptionsBuilder) configuration will be applied in addition to configuration performed here.

In order for the options to be passed into your context, you need to expose a constructor on your context that takes DbContextOptions<TContext> and passes it to the base constructor of DbContext.

lifetime
ServiceLifetime

The lifetime with which to register the factory and options. The default is Singleton

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.

For convenience, this method also registers the context type itself as a scoped service. This allows a context instance to be resolved from a dependency injection scope directly or created by the factory, as appropriate.

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 AddDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

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 factories for more information and examples.

Applies to