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

Adds the services required by the core of Entity Framework to an . 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.

Methods

  • AddEntityFramework(IServiceCollection)
    AddEntityFramework(IServiceCollection)

    Adds the services required by the core of Entity Framework to an . 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 AddEntityFramework(this IServiceCollection serviceCollection)<ExtensionAttribute>
    Public Shared Function AddEntityFramework(serviceCollection As IServiceCollection) As IServiceCollection

    Parameters

    • serviceCollection
      IServiceCollection
      IServiceCollection

      The to add services to.

    Returns

    • IServiceCollection
      IServiceCollection

      A builder that allows further Entity Framework specific setup of the .

    Remarks

    You only need to use this functionality when you want Entity Framework to resolve the services it uses from an external System.IServiceProvider. If you are not using an external System.IServiceProvider Entity Framework will take care of creating the services it requires.

    The database you are using will also define extension methods that can be called on the returned to register the services required by the database. For example, when using Microsoft.EntityFrameworkCore.SqlServer you would call collection.AddEntityFrameworkSqlServer().

    For derived contexts to be registered in the System.IServiceProvider and resolve their services from the System.IServiceProvider you must chain a call to the AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime) method on the returned .

    Example

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

Details

Assembly

Microsoft.EntityFrameworkCore.dll