Table of Contents

Class EntityFrameworkServiceCollectionExtensions

Namespace
Microsoft.Extensions.DependencyInjection
Assembly
Microsoft.EntityFrameworkCore.dll

Extension methods for setting up Entity Framework related services in an IServiceCollection.

public static class EntityFrameworkServiceCollectionExtensions
Inheritance
EntityFrameworkServiceCollectionExtensions
Inherited Members

Methods

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

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

public static IServiceCollection AddDbContextFactory<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime lifetime = ServiceLifetime.Singleton) where TContext : DbContext

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

IServiceCollection

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

Type Parameters

TContext

The type of DbContext to be created by the factory.

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.

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

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

public static IServiceCollection AddDbContextFactory<TContext>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, ServiceLifetime lifetime = ServiceLifetime.Singleton) where TContext : DbContext

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

IServiceCollection

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

Type Parameters

TContext

The type of DbContext to be created by the factory.

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.

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

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

public static IServiceCollection AddDbContextFactory<TContext, TFactory>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime lifetime = ServiceLifetime.Singleton) where TContext : DbContext where TFactory : IDbContextFactory<TContext>

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

IServiceCollection

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

Type Parameters

TContext

The type of DbContext to be created by the factory.

TFactory

The type of IDbContextFactory<TContext> to register.

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.

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

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

public static IServiceCollection AddDbContextFactory<TContext, TFactory>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime lifetime = ServiceLifetime.Singleton) where TContext : DbContext where TFactory : IDbContextFactory<TContext>

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

IServiceCollection

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

Type Parameters

TContext

The type of DbContext to be created by the factory.

TFactory

The type of IDbContextFactory<TContext> to register.

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.

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

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

public static IServiceCollection AddDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : DbContext

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 int

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

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.

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.

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

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

public static IServiceCollection AddDbContextPool<TContext>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : DbContext

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 int

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

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.

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>, int) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

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

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

public static IServiceCollection AddDbContextPool<TContextService, TContextImplementation>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContextService : class where TContextImplementation : DbContext, TContextService

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 int

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

Returns

IServiceCollection

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

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.

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.

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

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

public static IServiceCollection AddDbContextPool<TContextService, TContextImplementation>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContextService : class where TContextImplementation : DbContext, TContextService

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 int

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

Returns

IServiceCollection

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

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.

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>, int) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

public static IServiceCollection AddDbContext<TContext>(this IServiceCollection serviceCollection, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

contextLifetime ServiceLifetime

The lifetime with which to register the DbContext service in the container.

optionsLifetime ServiceLifetime

The lifetime with which to register the DbContextOptions service in the container.

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.

Remarks

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

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>?, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

public static IServiceCollection AddDbContext<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext

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.

contextLifetime ServiceLifetime

The lifetime with which to register the DbContext service in the container.

optionsLifetime ServiceLifetime

The lifetime with which to register the DbContextOptions service in the container.

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.

Remarks

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

AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider, DbContextOptionsBuilder>?, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

public static IServiceCollection AddDbContext<TContext>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContext : DbContext

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.

contextLifetime ServiceLifetime

The lifetime with which to register the DbContext service in the container.

optionsLifetime ServiceLifetime

The lifetime with which to register the DbContextOptions service in the container.

Returns

IServiceCollection

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

Type Parameters

TContext

The type of context to be registered.

Remarks

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.

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

AddDbContext<TContextService, TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

public static IServiceCollection AddDbContext<TContextService, TContextImplementation>(this IServiceCollection serviceCollection, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContextService : class where TContextImplementation : DbContext, TContextService

Parameters

serviceCollection IServiceCollection

The IServiceCollection to add services to.

contextLifetime ServiceLifetime

The lifetime with which to register the DbContext service in the container.

optionsLifetime ServiceLifetime

The lifetime with which to register the DbContextOptions service in the container.

Returns

IServiceCollection

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

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.

Remarks

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

AddDbContext<TContextService, TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>?, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

public static IServiceCollection AddDbContext<TContextService, TContextImplementation>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder>? optionsAction = null, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContextImplementation : DbContext, TContextService

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.

contextLifetime ServiceLifetime

The lifetime with which to register the DbContext service in the container.

optionsLifetime ServiceLifetime

The lifetime with which to register the DbContextOptions service in the container.

Returns

IServiceCollection

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

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.

Remarks

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

AddDbContext<TContextService, TContextImplementation>(IServiceCollection, Action<IServiceProvider, DbContextOptionsBuilder>?, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

public static IServiceCollection AddDbContext<TContextService, TContextImplementation>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder>? optionsAction, ServiceLifetime contextLifetime = ServiceLifetime.Scoped, ServiceLifetime optionsLifetime = ServiceLifetime.Scoped) where TContextImplementation : DbContext, TContextService

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.

contextLifetime ServiceLifetime

The lifetime with which to register the DbContext service in the container.

optionsLifetime ServiceLifetime

The lifetime with which to register the DbContextOptions service in the container.

Returns

IServiceCollection

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

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.

Remarks

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

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

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

public static IServiceCollection AddPooledDbContextFactory<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : DbContext

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 int

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

Returns

IServiceCollection

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

Type Parameters

TContext

The type of DbContext to be created by the factory.

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.

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

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

public static IServiceCollection AddPooledDbContextFactory<TContext>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : DbContext

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 int

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

Returns

IServiceCollection

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

Type Parameters

TContext

The type of DbContext to be created by the factory.

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.