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 を設定する場合に便利です。 既定では、他のオーバーロードを使用することをお勧めします。これにより、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 を設定する場合に便利です。 既定では、他のオーバーロードを使用することをお勧めします。これにより、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 を設定する場合に便利です。 既定では、 を使用 AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) することをお勧めします。これにより、Entity Framework は内部 Entity Framework サービス用に独自 IServiceProvider のを作成および維持できます。

適用対象

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

コンテナーからコンテキストを解決するために使用されるクラスまたはインターフェイス。

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

コンテナーからコンテキストを解決するために使用されるクラスまたはインターフェイス。

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

コンテナーからコンテキストを解決するために使用されるクラスまたはインターフェイス。

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 を設定する場合に便利です。 既定では、 を使用 AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) することをお勧めします。これにより、Entity Framework は内部 Entity Framework サービス用に独自 IServiceProvider のを作成および維持できます。

適用対象