Sdílet prostřednictvím


EntityFrameworkServiceCollectionExtensions.ConfigureDbContext Method

Definition

Overloads

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

Configures the given context type in the IServiceCollection.

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

Configures the given context type in the IServiceCollection.

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

Source:
EntityFrameworkServiceCollectionExtensions.cs

Configures the given context type in the IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureDbContext<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member ConfigureDbContext : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function ConfigureDbContext(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

Type Parameters

TContext

The type of context to be registered.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

optionsAction
Action<DbContextOptionsBuilder>

An action to configure the DbContextOptions for the context.

optionsLifetime
ServiceLifetime

The lifetime with which the DbContextOptions service will be registered in the container.

Returns

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

Remarks

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime), AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32), AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) or AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32) must also be called for the specified configuration to take effect. Calling this method after any of the above will ovewrite conflicting configuration. For non-pooled contexts OnConfiguring(DbContextOptionsBuilder) configuration will be applied in addition to configuration performed here.

This method can be invoked multiple times and the configuration will be applied in the given order.

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

Applies to

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

Source:
EntityFrameworkServiceCollectionExtensions.cs

Configures the given context type in the IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureDbContext<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member ConfigureDbContext : 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 ConfigureDbContext(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) 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>

An action to configure the DbContextOptions for the context.

optionsLifetime
ServiceLifetime

The lifetime with which the DbContextOptions service will be registered in the container.

Returns

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

Remarks

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime), AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32), AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) or AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32) must also be called for the specified configuration to take effect. Calling this method after any of the above will ovewrite conflicting configuration. For non-pooled contexts OnConfiguring(DbContextOptionsBuilder) configuration will be applied in addition to configuration performed here.

This method can be invoked multiple times and the configuration will be applied in the given order.

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

Applies to