EntityFrameworkServiceCollectionExtensions EntityFrameworkServiceCollectionExtensions Class


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

public static class EntityFrameworkServiceCollectionExtensions
Public Module EntityFrameworkServiceCollectionExtensions


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)
Public Shared Function AddEntityFramework(serviceCollection As IServiceCollection) As IServiceCollection
IServiceCollection IServiceCollection

The to add services to.

IServiceCollection IServiceCollection

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


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

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


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 .

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