EntityFrameworkServiceCollectionExtensions EntityFrameworkServiceCollectionExtensions Class

Definition

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

public static class EntityFrameworkServiceCollectionExtensions
Public Module EntityFrameworkServiceCollectionExtensions
Inheritance
System.ObjectSystem.Object
EntityFrameworkServiceCollectionExtensionsEntityFrameworkServiceCollectionExtensions

Methods

AddDbContext(IServiceCollection, Action, ServiceLifetime) AddDbContext(Of TContext)(IServiceCollection, Action(Of DbContextOptionsBuilder), ServiceLifetime)

Registers the given context as a service in the . 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 IServiceCollection AddDbContext<TContext>(this IServiceCollection serviceCollection, Action<DbContextOptionsBuilder> optionsAction = null, ServiceLifetime contextLifetime = null)where TContext : DbContext
<ExtensionAttribute>
Public Shared Function AddDbContext(Of TContext As DbContext)(serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder) = Nothing, contextLifetime As ServiceLifetime = Nothing) As IServiceCollection
Type Parameters
TContext

The type of context to be registered.

Parameters
serviceCollection
IServiceCollection IServiceCollection

The to add services to.

optionsAction
System.Action<DbContextOptionsBuilder> System.Action(Of 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

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

Returns
IServiceCollection IServiceCollection

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

Registers the given context as a service in the . 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 System.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 System.IServiceProvider for internal Entity Framework services.

public static IServiceCollection AddDbContext<TContext>(this IServiceCollection serviceCollection, Action<IServiceProvider, DbContextOptionsBuilder> optionsAction, ServiceLifetime contextLifetime = null)where TContext : DbContext
<ExtensionAttribute>
Public Shared Function AddDbContext(Of TContext As DbContext)(serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), contextLifetime As ServiceLifetime = Nothing) As IServiceCollection
Type Parameters
TContext

The type of context to be registered.

Parameters
serviceCollection
IServiceCollection IServiceCollection

The to add services to.

optionsAction
System.Action<System.IServiceProvider, DbContextOptionsBuilder> System.Action(Of System.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

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

Returns
IServiceCollection IServiceCollection

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

Registers the given context as a service in the . 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 IServiceCollection AddDbContext<TContext>(this IServiceCollection serviceCollection, ServiceLifetime contextLifetime)where TContext : DbContext
<ExtensionAttribute>
Public Shared 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 IServiceCollection

The to add services to.

contextLifetime
ServiceLifetime ServiceLifetime

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

Returns
IServiceCollection IServiceCollection

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

Equals(Object) Equals(Object)

Inherited from System.Object

Equals(Object, Object) Equals(Object, Object)

Inherited from System.Object

GetHashCode() GetHashCode()

Inherited from System.Object

GetType() GetType()

Inherited from System.Object

MemberwiseClone() MemberwiseClone()

Inherited from System.Object

ReferenceEquals(Object, Object) ReferenceEquals(Object, Object)

Inherited from System.Object

ToString() ToString()

Inherited from System.Object