Language

EntityFrameworkServiceCollectionExtensions.AddDbContext メソッド

定義

オーバーロード

Name 説明
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.

このオーバーロードには、アプリケーションIServiceProviderを提供するoptionsActionがあります。 これは、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)

ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs

指定されたコンテキストをサービスとして 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.

このオーバーロードには、アプリケーションIServiceProviderを提供するoptionsActionがあります。 これは、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)

ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs

指定されたコンテキストをサービスとして 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)

ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs

指定されたコンテキストをサービスとして 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 スレッドの問題の回避に 関するページを参照してください。

詳細と例については、 依存関係の挿入での DbContext の使用 を参照してください。

このオーバーロードには、アプリケーションのIServiceProviderを提供するoptionsActionがあります。 これは、プライマリ アプリケーション サービス プロバイダーから内部サービスを解決するためにEntity Framework Coreを設定する場合に便利です。 既定では、Entity Framework が内部 Entity Framework サービス用の独自のIServiceProviderを作成して維持できるようにするAddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)を使用することをお勧めします。

適用対象

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

ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs

指定されたコンテキストをサービスとして 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)

ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs

指定されたコンテキストをサービスとして 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)

ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs
ソース:
EntityFrameworkServiceCollectionExtensions.cs

指定されたコンテキストをサービスとして 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 の使用 を参照してください。

このオーバーロードには、アプリケーションのIServiceProviderを提供するoptionsActionがあります。 これは、プライマリ アプリケーション サービス プロバイダーから内部サービスを解決するためにEntity Framework Coreを設定する場合に便利です。 既定では、Entity Framework が内部 Entity Framework サービス用の独自のIServiceProviderを作成して維持できるようにするAddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)を使用することをお勧めします。

適用対象