EntityFrameworkServiceCollectionExtensions.AddDbContext 메서드

정의

오버로드

AddDbContext<TContext>(IServiceCollection, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection. 애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET 사용) 이 메서드를 사용합니다. 종속성 주입 설정에 대한 자세한 내용은 을 참조하세요 http://go.microsoft.com/fwlink/?LinkId=526890.

AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection. 애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET 사용) 이 메서드를 사용합니다. 종속성 주입 설정에 대한 자세한 내용은 을 참조하세요 http://go.microsoft.com/fwlink/?LinkId=526890.

AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection. 애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET 사용) 이 메서드를 사용합니다. 종속성 주입 설정에 대한 자세한 내용은 http://go.microsoft.com/fwlink/?LinkId=526890.

이 오버로드에는 애플리케이션을 optionsAction 제공하는 가 있습니다 IServiceProvider. 이 기능은 Entity Framework를 설정하여 기본 애플리케이션 서비스 공급자에서 내부 서비스를 resolve 경우에 유용합니다. 기본적으로 Entity Framework가 내부 Entity Framework 서비스에 대해 자체 IServiceProvider 적으로 만들고 유지 관리할 수 있도록 하는 다른 오버로드를 사용하는 것이 좋습니다.

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

AddDbContext<TContext>(IServiceCollection, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection. 애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET 사용) 이 메서드를 사용합니다. 종속성 주입 설정에 대한 자세한 내용은 을 참조하세요 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

형식 매개 변수

TContext

등록할 컨텍스트의 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

    services.AddDbContext<MyContext>(ServiceLifetime.Scoped); 
}

적용 대상

AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

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

형식 매개 변수

TContext

등록할 컨텍스트의 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

optionsLifetime
ServiceLifetime

컨테이너에 DbContextOptions 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

    services.AddDbContext<MyContext>(ServiceLifetime.Scoped);
}

설명

애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET Core 사용) 이 메서드를 사용합니다. 종속성 주입을 사용하지 않는 애플리케이션의 경우 생성자를 사용하여 직접 인스턴스를 만드는 DbContext 것이 좋습니다. 그런 다음 메서드를 재정의 OnConfiguring(DbContextOptionsBuilder) 하여 연결 문자열 및 기타 옵션을 구성할 수 있습니다.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

자세한 내용과 예제는 종속성 주입과 함께 DbContext 사용을 참조하세요.

적용 대상

AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection. 애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET 사용) 이 메서드를 사용합니다. 종속성 주입 설정에 대한 자세한 내용은 을 참조하세요 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 = default, 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) = Nothing, Optional contextLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) As IServiceCollection

형식 매개 변수

TContext

등록할 컨텍스트의 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

optionsAction
Action<DbContextOptionsBuilder>

컨텍스트에 대해 를 DbContextOptions 구성하는 선택적 작업입니다. 파생 컨텍스트에서 메서드를 재정의하여 컨텍스트의 구성을 OnConfiguring(DbContextOptionsBuilder) 수행하는 대신 사용할 수 있습니다.

여기서 작업이 제공된 경우 메서드는 OnConfiguring(DbContextOptionsBuilder) 파생 컨텍스트에서 재정의된 경우에도 계속 실행됩니다. OnConfiguring(DbContextOptionsBuilder) 구성은 여기에서 수행되는 구성 외에도 적용됩니다.

옵션을 컨텍스트에 전달하려면 를 사용하여 DbContextOptions<TContext> 의 기본 생성자에 전달하는 생성자를 DbContext컨텍스트에 노출해야 합니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection. 애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET 사용) 이 메서드를 사용합니다. 종속성 주입 설정에 대한 자세한 내용은 http://go.microsoft.com/fwlink/?LinkId=526890.

이 오버로드에는 애플리케이션을 optionsAction 제공하는 가 있습니다 IServiceProvider. 이 기능은 Entity Framework를 설정하여 기본 애플리케이션 서비스 공급자에서 내부 서비스를 resolve 경우에 유용합니다. 기본적으로 Entity Framework가 내부 Entity Framework 서비스에 대해 자체 IServiceProvider 적으로 만들고 유지 관리할 수 있도록 하는 다른 오버로드를 사용하는 것이 좋습니다.

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

형식 매개 변수

TContext

등록할 컨텍스트의 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

컨텍스트에 대해 를 DbContextOptions 구성하는 선택적 작업입니다. 파생 컨텍스트에서 메서드를 재정의하여 컨텍스트의 구성을 OnConfiguring(DbContextOptionsBuilder) 수행하는 대신 사용할 수 있습니다.

여기서 작업이 제공된 경우 메서드는 OnConfiguring(DbContextOptionsBuilder) 파생 컨텍스트에서 재정의된 경우에도 계속 실행됩니다. OnConfiguring(DbContextOptionsBuilder) 구성은 여기에서 수행되는 구성 외에도 적용됩니다.

옵션을 컨텍스트에 전달하려면 를 사용하여 DbContextOptions<TContext> 의 기본 생성자에 전달하는 생성자를 DbContext컨텍스트에 노출해야 합니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, 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;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContext> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, 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) = Nothing, Optional contextLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) As IServiceCollection

형식 매개 변수

TContext

등록할 컨텍스트의 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

optionsAction
Action<DbContextOptionsBuilder>

컨텍스트에 대해 를 DbContextOptions 구성하는 선택적 작업입니다. 파생 컨텍스트에서 메서드를 재정의하여 컨텍스트의 구성을 OnConfiguring(DbContextOptionsBuilder) 수행하는 대신 사용할 수 있습니다.

여기서 작업이 제공된 경우 메서드는 OnConfiguring(DbContextOptionsBuilder) 파생 컨텍스트에서 재정의된 경우에도 계속 실행됩니다. OnConfiguring(DbContextOptionsBuilder) 구성은 여기에서 수행되는 구성 외에도 적용됩니다.

옵션을 컨텍스트에 전달하려면 를 사용하여 DbContextOptions<TContext> 의 기본 생성자에 전달하는 생성자를 DbContext컨텍스트에 노출해야 합니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

optionsLifetime
ServiceLifetime

컨테이너에 DbContextOptions 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

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

설명

애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET Core 사용) 이 메서드를 사용합니다. 종속성 주입을 사용하지 않는 애플리케이션의 경우 생성자를 사용하여 직접 인스턴스를 만드는 DbContext 것이 좋습니다. 그런 다음 메서드를 재정의 OnConfiguring(DbContextOptionsBuilder) 하여 연결 문자열 및 기타 옵션을 구성할 수 있습니다.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

자세한 내용과 예제는 종속성 주입과 함께 DbContext 사용을 참조하세요.

적용 대상

AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

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

형식 매개 변수

TContext

등록할 컨텍스트의 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

컨텍스트에 대해 를 DbContextOptions 구성하는 선택적 작업입니다. 파생 컨텍스트에서 메서드를 재정의하여 컨텍스트의 구성을 OnConfiguring(DbContextOptionsBuilder) 수행하는 대신 사용할 수 있습니다.

여기서 작업이 제공된 경우 메서드는 OnConfiguring(DbContextOptionsBuilder) 파생 컨텍스트에서 재정의된 경우에도 계속 실행됩니다. OnConfiguring(DbContextOptionsBuilder) 구성은 여기에서 수행되는 구성 외에도 적용됩니다.

옵션을 컨텍스트에 전달하려면 를 사용하여 DbContextOptions<TContext> 의 기본 생성자에 전달하는 생성자를 DbContext컨텍스트에 노출해야 합니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

optionsLifetime
ServiceLifetime

컨테이너에 DbContextOptions 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

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

설명

애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET Core 사용) 이 메서드를 사용합니다. 종속성 주입을 사용하지 않는 애플리케이션의 경우 생성자를 사용하여 직접 인스턴스를 만드는 DbContext 것이 좋습니다. 그런 다음 메서드를 재정의 OnConfiguring(DbContextOptionsBuilder) 하여 연결 문자열 및 기타 옵션을 구성할 수 있습니다.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

자세한 내용과 예제는 종속성 주입과 함께 DbContext 사용을 참조하세요.

이 오버로드에는 optionsAction 애플리케이션의 IServiceProvider를 제공하는 가 있습니다. 이 기능은 Entity Framework Core를 설정하여 기본 애플리케이션 서비스 공급자에서 내부 서비스를 resolve 경우에 유용합니다. 기본적으로 Entity Framework가 내부 Entity Framework 서비스에 대해 자체 IServiceProvider 적으로 만들고 유지 관리할 수 있도록 하는 를 사용하는 AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) 것이 좋습니다.

적용 대상

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

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

형식 매개 변수

TContextService

컨테이너에서 컨텍스트를 resolve 데 사용할 클래스 또는 인터페이스입니다.

TContextImplementation

만들 구체적인 구현 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

optionsLifetime
ServiceLifetime

컨테이너에 DbContextOptions 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

    services.AddDbContext<MyContext>(ServiceLifetime.Scoped);
}

설명

애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET Core 사용) 이 메서드를 사용합니다. 종속성 주입을 사용하지 않는 애플리케이션의 경우 생성자를 사용하여 직접 인스턴스를 만드는 DbContext 것이 좋습니다. 그런 다음 메서드를 재정의 OnConfiguring(DbContextOptionsBuilder) 하여 연결 문자열 및 기타 옵션을 구성할 수 있습니다.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

자세한 내용과 예제는 종속성 주입과 함께 DbContext 사용을 참조하세요.

적용 대상

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, 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;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContext<TContextService,TContextImplementation> (this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, 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) = Nothing, Optional contextLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped, Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Scoped) As IServiceCollection

형식 매개 변수

TContextService

컨테이너에서 컨텍스트를 resolve 데 사용할 클래스 또는 인터페이스입니다.

TContextImplementation

만들 구체적인 구현 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

optionsAction
Action<DbContextOptionsBuilder>

컨텍스트에 대해 를 DbContextOptions 구성하는 선택적 작업입니다. 파생 컨텍스트에서 메서드를 재정의하여 컨텍스트의 구성을 OnConfiguring(DbContextOptionsBuilder) 수행하는 대신 사용할 수 있습니다.

여기서 작업이 제공된 경우 메서드는 OnConfiguring(DbContextOptionsBuilder) 파생 컨텍스트에서 재정의된 경우에도 계속 실행됩니다. OnConfiguring(DbContextOptionsBuilder) 구성은 여기에서 수행되는 구성 외에도 적용됩니다.

옵션을 컨텍스트에 전달하려면 를 사용하여 DbContextOptions<TContext> 의 기본 생성자에 전달하는 생성자를 DbContext컨텍스트에 노출해야 합니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

optionsLifetime
ServiceLifetime

컨테이너에 DbContextOptions 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

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

설명

애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET Core 사용) 이 메서드를 사용합니다. 종속성 주입을 사용하지 않는 애플리케이션의 경우 생성자를 사용하여 직접 인스턴스를 만드는 DbContext 것이 좋습니다. 그런 다음 메서드를 재정의 OnConfiguring(DbContextOptionsBuilder) 하여 연결 문자열 및 기타 옵션을 구성할 수 있습니다.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

자세한 내용과 예제는 종속성 주입과 함께 DbContext 사용을 참조하세요.

적용 대상

AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)

지정된 컨텍스트를 에 서비스로 등록합니다 IServiceCollection.

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

형식 매개 변수

TContextService

컨테이너에서 컨텍스트를 resolve 데 사용할 클래스 또는 인터페이스입니다.

TContextImplementation

만들 구체적인 구현 형식입니다.

매개 변수

serviceCollection
IServiceCollection

서비스를 추가할 IServiceCollection입니다.

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

컨텍스트에 대해 를 DbContextOptions 구성하는 선택적 작업입니다. 파생 컨텍스트에서 메서드를 재정의하여 컨텍스트의 구성을 OnConfiguring(DbContextOptionsBuilder) 수행하는 대신 사용할 수 있습니다.

여기서 작업이 제공된 경우 메서드는 OnConfiguring(DbContextOptionsBuilder) 파생 컨텍스트에서 재정의된 경우에도 계속 실행됩니다. OnConfiguring(DbContextOptionsBuilder) 구성은 여기에서 수행되는 구성 외에도 적용됩니다.

옵션을 컨텍스트에 전달하려면 를 사용하여 DbContextOptions<TContext> 의 기본 생성자에 전달하는 생성자를 DbContext컨텍스트에 노출해야 합니다.

contextLifetime
ServiceLifetime

컨테이너에 DbContext 서비스를 등록할 수명입니다.

optionsLifetime
ServiceLifetime

컨테이너에 DbContextOptions 서비스를 등록할 수명입니다.

반환

여러 호출을 연결할 수 있도록 동일한 서비스 컬렉션입니다.

예제

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

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

설명

애플리케이션에서 종속성 주입을 사용하는 경우(예: ASP.NET Core 사용) 이 메서드를 사용합니다. 종속성 주입을 사용하지 않는 애플리케이션의 경우 생성자를 사용하여 직접 인스턴스를 만드는 DbContext 것이 좋습니다. 그런 다음 메서드를 재정의 OnConfiguring(DbContextOptionsBuilder) 하여 연결 문자열 및 기타 옵션을 구성할 수 있습니다.

Entity Framework Core는 동일한 DbContext 인스턴스에서 실행되는 여러 병렬 작업을 지원하지 않습니다. 여기에는 비동기 쿼리의 병렬 실행과 여러 스레드에서의 명시적 동시 사용이 모두 포함됩니다. 따라서 항상 즉시 비동기 호출을 기다리거나 병렬로 실행되는 작업에 별도의 DbContext 인스턴스를 사용합니다. 자세한 내용 및 예제 는 DbContext 스레딩 문제 방지 를 참조하세요.

자세한 내용과 예제는 종속성 주입과 함께 DbContext 사용을 참조하세요.

이 오버로드에는 optionsAction 애플리케이션의 IServiceProvider를 제공하는 가 있습니다. 이 기능은 Entity Framework Core를 설정하여 기본 애플리케이션 서비스 공급자에서 내부 서비스를 resolve 경우에 유용합니다. 기본적으로 Entity Framework가 내부 Entity Framework 서비스에 대해 자체 IServiceProvider 적으로 만들고 유지 관리할 수 있도록 하는 를 사용하는 AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) 것이 좋습니다.

적용 대상