EntityFrameworkServiceCollectionExtensions.AddDbContext EntityFrameworkServiceCollectionExtensions.AddDbContext EntityFrameworkServiceCollectionExtensions.AddDbContext Method

Definition

Overloads

AddDbContext<TContext>(IServiceCollection, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, ServiceLifetime) 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) AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, ServiceLifetime, 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<DbContextOptionsBuilder>, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) 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) AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime) 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) AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, 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, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, 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<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, 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<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, 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<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, 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, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, ServiceLifetime) 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

contextLifetime
ServiceLifetime ServiceLifetime 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); 
          }

AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, ServiceLifetime, 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, 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

contextLifetime
ServiceLifetime ServiceLifetime ServiceLifetime

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

optionsLifetime
ServiceLifetime ServiceLifetime 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);
            }

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) 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 = null, 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) = null, Optional contextLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) As IServiceCollection

Type Parameters

TContext

The type of context to be registered.

Parameters

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 ServiceLifetime 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)); 
          }

AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime) 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

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 ServiceLifetime 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)); 
          }

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, 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 = null, 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) = null, 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

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 ServiceLifetime ServiceLifetime

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

optionsLifetime
ServiceLifetime ServiceLifetime 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));
            }

AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, 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, 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

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 ServiceLifetime ServiceLifetime

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

optionsLifetime
ServiceLifetime ServiceLifetime 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));
           }

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

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

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, 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<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

contextLifetime
ServiceLifetime ServiceLifetime ServiceLifetime

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

optionsLifetime
ServiceLifetime ServiceLifetime 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);
            }

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, 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<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = null, 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) = null, 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

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 ServiceLifetime ServiceLifetime

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

optionsLifetime
ServiceLifetime ServiceLifetime 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));
            }

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, 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<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

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 ServiceLifetime ServiceLifetime

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

optionsLifetime
ServiceLifetime ServiceLifetime 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));
            }

Applies to