Freigeben über


EntityFrameworkServiceCollectionExtensions.AddDbContext Method

Definition

Overloads

AddDbContext<TContext>(IServiceCollection, ServiceLifetime)

Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime)

Registers the given context as a service in the IServiceCollection.

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

Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

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

Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

This overload has an optionsAction that provides the applications IServiceProvider. This is useful if you want to setup Entity Framework to resolve its internal services from the primary application service provider. By default, we recommend using the other overload, which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

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

Registers the given context as a service in the IServiceCollection.

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

Registers the given context as a service in the IServiceCollection.

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

Registers the given context as a service in the IServiceCollection.

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

Registers the given context as a service in the IServiceCollection.

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

Registers the given context as a service in the IServiceCollection.

AddDbContext<TContext>(IServiceCollection, ServiceLifetime)

Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Microsoft.Extensions.DependencyInjection.ServiceLifetime contextLifetime) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContext : Microsoft.Extensions.DependencyInjection.IServiceCollection * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContext(Of TContext As DbContext) (serviceCollection As IServiceCollection, contextLifetime As ServiceLifetime) As IServiceCollection

Type Parameters

TContext

The type of context to be registered.

Parameters

serviceCollection
IServiceCollection

The IServiceCollection to add services to.

contextLifetime
ServiceLifetime

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

Returns

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

Examples

public void ConfigureServices(IServiceCollection services) 
{
    var connectionString = "connection string to database";

    services.AddDbContext<MyContext>(ServiceLifetime.Scoped); 
}

Applies to

AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime)

Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs

Registers the given context as a service in the IServiceCollection.

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

Type Parameters

TContext

The type of context to be registered.

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

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddDbContext<MyContext>(ServiceLifetime.Scoped);
}

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.

Applies to

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

Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

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

Returns

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

Examples

public void ConfigureServices(IServiceCollection services) 
{
    var connectionString = "connection string to database";

    services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString)); 
}

Applies to

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

Registers the given context as a service in the IServiceCollection. You use this method when using dependency injection in your application, such as with ASP.NET. For more information on setting up dependency injection, see http://go.microsoft.com/fwlink/?LinkId=526890.

This overload has an optionsAction that provides the applications IServiceProvider. This is useful if you want to setup Entity Framework to resolve its internal services from the primary application service provider. By default, we recommend using the other overload, which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.

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

Returns

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

Examples

public void ConfigureServices(IServiceCollection services) 
{
    var connectionString = "connection string to database";

    services
        .AddEntityFrameworkSqlServer()
        .AddDbContext<MyContext>((serviceProvider, options) => 
            options.UseSqlServer(connectionString)
                   .UseInternalServiceProvider(serviceProvider)); 
}

Applies to

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

Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs

Registers the given context as a service in the IServiceCollection.

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

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));
}

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.

Applies to

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

Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs

Registers the given context as a service in the IServiceCollection.

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

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services
        .AddEntityFrameworkSqlServer()
        .AddDbContext<MyContext>((serviceProvider, options) =>
            options.UseSqlServer(connectionString)
                   .UseInternalServiceProvider(serviceProvider));
}

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.

Applies to

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

Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs

Registers the given context as a service in the IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Microsoft.Extensions.DependencyInjection.ServiceLifetime contextLifetime, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) where TContextService : class where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
static member AddDbContext : Microsoft.Extensions.DependencyInjection.IServiceCollection * Microsoft.Extensions.DependencyInjection.ServiceLifetime * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'ContextService : null and 'ContextImplementation :> Microsoft.EntityFrameworkCore.DbContext and 'ContextImplementation :> 'ContextService)
<Extension()>
Public Function AddDbContext(Of TContextService As Class, TContextImplementation As Class) (serviceCollection As IServiceCollection, contextLifetime As ServiceLifetime, Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) 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.

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

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddDbContext<MyContext>(ServiceLifetime.Scoped);
}

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.

Applies to

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

Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs

Registers the given context as a service in the IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime contextLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime contextLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
static member AddDbContext : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'ContextImplementation :> Microsoft.EntityFrameworkCore.DbContext and 'ContextImplementation :> 'ContextService)
<Extension()>
Public Function AddDbContext(Of TContextService, TContextImplementation) (serviceCollection As IServiceCollection, Optional optionsAction As Action(Of DbContextOptionsBuilder) = Nothing, Optional contextLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) 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>

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

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services.AddDbContext<MyContext>(options => options.UseSqlServer(connectionString));
}

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.

Applies to

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

Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs
Source:
EntityFrameworkServiceCollectionExtensions.cs

Registers the given context as a service in the IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime contextLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime contextLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) where TContextImplementation : Microsoft.EntityFrameworkCore.DbContext, TContextService;
static member AddDbContext : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'ContextImplementation :> Microsoft.EntityFrameworkCore.DbContext and 'ContextImplementation :> 'ContextService)
<Extension()>
Public Function AddDbContext(Of TContextService, TContextImplementation) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional contextLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) 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>

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

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

Examples

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = "connection string to database";

    services
        .AddEntityFrameworkSqlServer()
        .AddDbContext<MyContext>((serviceProvider, options) =>
            options.UseSqlServer(connectionString)
                   .UseInternalServiceProvider(serviceProvider));
}

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.

Applies to