EntityFrameworkServiceCollectionExtensions EntityFrameworkServiceCollectionExtensions Class

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

Syntax

Declaration

public static class EntityFrameworkServiceCollectionExtensionsPublic Module EntityFrameworkServiceCollectionExtensions

Inheritance Hierarchy

  • System.Object
    System.Object
  • EntityFrameworkServiceCollectionExtensions
    EntityFrameworkServiceCollectionExtensions

Inherited Members

ToString(),
ToString(),
Equals(Object),
Equals(Object),
Equals(Object, Object),
Equals(Object, Object),
ReferenceEquals(Object, Object),
ReferenceEquals(Object, Object),
GetHashCode(),
GetHashCode(),
GetType(),
GetType(),
MemberwiseClone()
MemberwiseClone()

Methods summary

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.

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.

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.

Methods

  • AddDbContext<TContext>(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

    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.

    Example

        public void ConfigureServices(IServiceCollection services) 
        {
            var connectionString = "connection string to database";
    
            services.AddDbContext<MyContext>(ServiceLifetime.Scoped); 
        }
  • AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, 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

    Parameters

    Returns

    • IServiceCollection
      IServiceCollection

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

    Example

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

    Parameters

    • serviceCollection
      IServiceCollection
      IServiceCollection

      The to add services to.

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

    Example

        public void ConfigureServices(IServiceCollection services) 
        {
            var connectionString = "connection string to database";
    
            services
                .AddEntityFrameworkSqlServer()
                .AddDbContext<MyContext>((serviceProvider, options) => 
                    options.UseSqlServer(connectionString)
                           .UseInternalServiceProvider(serviceProvider)); 
        }

Details

Assembly

Microsoft.EntityFrameworkCore.dll