Language

EntityFrameworkServiceCollectionExtensions.AddDbContextFactory メソッド

定義

オーバーロード

Name 説明
AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

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

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

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

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)

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

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext and 'Factory :> Microsoft.EntityFrameworkCore.IDbContextFactory<'Context>)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext, TFactory As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

型パラメーター

TContext

ファクトリによって作成される DbContext の種類。

TFactory

登録する IDbContextFactory<TContext> の種類。

パラメーター

serviceCollection
IServiceCollection

サービスを追加する IServiceCollection 。

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

コンテキストの DbContextOptions を構成するオプションのアクション。 これにより、派生コンテキストで OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドすることで、コンテキストの構成を実行する代わりに使用できます。

ここでアクションが指定されている場合、派生コンテキストでオーバーライドされた場合でも、 OnConfiguring(DbContextOptionsBuilder) メソッドは実行されます。 OnConfiguring(DbContextOptionsBuilder) 構成は、ここで実行される構成に加えて適用されます。

オプションをコンテキストに渡すには、 DbContextOptions<TContext> を受け取り、それを DbContextの基本コンストラクターに渡すコンストラクターをコンテキストで公開する必要があります。

lifetime
ServiceLifetime

ファクトリとオプションを登録する有効期間。 既定値は Singleton です

戻り値

複数の呼び出しをチェーンできるように、同じサービス コレクション。

注釈

コンテキスト型を直接登録する代わりにファクトリを登録すると、新しい DbContext インスタンスを簡単に作成できます。 Blazor アプリケーションやその他の状況で依存関係の挿入スコープがコンテキストの有効期間と一致しない場合は、ファクトリの登録をお勧めします。

Blazor など、アプリケーションで依存関係の挿入を使用する場合は、このメソッドを使用します。 依存関係の挿入を使用しないアプリケーションの場合は、コンストラクターを使用して DbContext インスタンスを直接作成することを検討してください。 その後、OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドして、接続文字列やその他のオプションを構成できます。

便宜上、このメソッドはコンテキスト型自体をスコープサービスとして登録します。 これにより、必要に応じて、コンテキスト インスタンスを依存関係挿入スコープから直接解決したり、ファクトリによって作成したりできます。

このオーバーロードにより、EF Core に付属する既定のファクトリを使用する代わりに、 IDbContextFactory<TContext> の特定の実装を登録できます。

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

Entity Framework Core では、同じ DbContext インスタンス上での複数の並列操作の実行がサポートされていません。 これには、非同期クエリの並列実行と、複数のスレッドからの明示的な同時使用の両方が含まれます。 したがって、常に非同期呼び出しをすぐに待機するか、並列で実行される操作に個別の DbContext インスタンスを使用します。 詳細と例については、 DbContext スレッドの問題の回避に 関するページを参照してください。

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

適用対象

AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)

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

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext,TFactory>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext where TFactory : Microsoft.EntityFrameworkCore.IDbContextFactory<TContext>;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext and 'Factory :> Microsoft.EntityFrameworkCore.IDbContextFactory<'Context>)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext, TFactory As DbContext) (serviceCollection As IServiceCollection, Optional optionsAction As Action(Of DbContextOptionsBuilder) = Nothing, Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

型パラメーター

TContext

ファクトリによって作成される DbContext の種類。

TFactory

登録する IDbContextFactory<TContext> の種類。

パラメーター

serviceCollection
IServiceCollection

サービスを追加する IServiceCollection 。

optionsAction
Action<DbContextOptionsBuilder>

コンテキストの DbContextOptions を構成するオプションのアクション。 これにより、派生コンテキストで OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドすることで、コンテキストの構成を実行する代わりに使用できます。

ここでアクションが指定されている場合、派生コンテキストでオーバーライドされた場合でも、 OnConfiguring(DbContextOptionsBuilder) メソッドは実行されます。 OnConfiguring(DbContextOptionsBuilder) 構成は、ここで実行される構成に加えて適用されます。

オプションをコンテキストに渡すには、 DbContextOptions<TContext> を受け取り、それを DbContextの基本コンストラクターに渡すコンストラクターをコンテキストで公開する必要があります。

lifetime
ServiceLifetime

ファクトリとオプションを登録する有効期間。 既定値は Singleton です

戻り値

複数の呼び出しをチェーンできるように、同じサービス コレクション。

注釈

コンテキスト型を直接登録する代わりにファクトリを登録すると、新しい DbContext インスタンスを簡単に作成できます。 Blazor アプリケーションやその他の状況で依存関係の挿入スコープがコンテキストの有効期間と一致しない場合は、ファクトリの登録をお勧めします。

Blazor など、アプリケーションで依存関係の挿入を使用する場合は、このメソッドを使用します。 依存関係の挿入を使用しないアプリケーションの場合は、コンストラクターを使用して DbContext インスタンスを直接作成することを検討してください。 その後、OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドして、接続文字列やその他のオプションを構成できます。

便宜上、このメソッドはコンテキスト型自体をスコープサービスとして登録します。 これにより、必要に応じて、コンテキスト インスタンスを依存関係挿入スコープから直接解決したり、ファクトリによって作成したりできます。

このオーバーロードにより、EF Core に付属する既定のファクトリを使用する代わりに、 IDbContextFactory<TContext> の特定の実装を登録できます。

Entity Framework Core では、同じ DbContext インスタンス上での複数の並列操作の実行がサポートされていません。 これには、非同期クエリの並列実行と、複数のスレッドからの明示的な同時使用の両方が含まれます。 したがって、常に非同期呼び出しをすぐに待機するか、並列で実行される操作に個別の DbContext インスタンスを使用します。 詳細と例については、 DbContext スレッドの問題の回避に 関するページを参照してください。

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

適用対象

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

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

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder>? optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction = default, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, Optional optionsAction As Action(Of DbContextOptionsBuilder) = Nothing, Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

型パラメーター

TContext

ファクトリによって作成される DbContext の種類。

パラメーター

serviceCollection
IServiceCollection

サービスを追加する IServiceCollection 。

optionsAction
Action<DbContextOptionsBuilder>

コンテキストの DbContextOptions を構成するオプションのアクション。 これにより、派生コンテキストで OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドすることで、コンテキストの構成を実行する代わりに使用できます。

ここでアクションが指定されている場合、派生コンテキストでオーバーライドされた場合でも、 OnConfiguring(DbContextOptionsBuilder) メソッドは実行されます。 OnConfiguring(DbContextOptionsBuilder) 構成は、ここで実行される構成に加えて適用されます。

オプションをコンテキストに渡すには、 DbContextOptions<TContext> を受け取り、それを DbContextの基本コンストラクターに渡すコンストラクターをコンテキストで公開する必要があります。

lifetime
ServiceLifetime

ファクトリとオプションを登録する有効期間。 既定値は Singleton です

戻り値

複数の呼び出しをチェーンできるように、同じサービス コレクション。

注釈

コンテキスト型を直接登録する代わりにファクトリを登録すると、新しい DbContext インスタンスを簡単に作成できます。 Blazor アプリケーションやその他の状況で依存関係の挿入スコープがコンテキストの有効期間と一致しない場合は、ファクトリの登録をお勧めします。

Blazor など、アプリケーションで依存関係の挿入を使用する場合は、このメソッドを使用します。 依存関係の挿入を使用しないアプリケーションの場合は、コンストラクターを使用して DbContext インスタンスを直接作成することを検討してください。 その後、OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドして、接続文字列やその他のオプションを構成できます。

便宜上、このメソッドはコンテキスト型自体をスコープサービスとして登録します。 これにより、必要に応じて、コンテキスト インスタンスを依存関係挿入スコープから直接解決したり、ファクトリによって作成したりできます。

Entity Framework Core では、同じ DbContext インスタンス上での複数の並列操作の実行がサポートされていません。 これには、非同期クエリの並列実行と、複数のスレッドからの明示的な同時使用の両方が含まれます。 したがって、常に非同期呼び出しをすぐに待機するか、並列で実行される操作に個別の DbContext インスタンスを使用します。 詳細と例については、 DbContext スレッドの問題の回避に 関するページを参照してください。

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

適用対象

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

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

IServiceCollectionにIDbContextFactory<TContext>を登録して、指定されたDbContext型のインスタンスを作成します。

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime lifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddDbContextFactory : 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 AddDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional lifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection

型パラメーター

TContext

ファクトリによって作成される DbContext の種類。

パラメーター

serviceCollection
IServiceCollection

サービスを追加する IServiceCollection 。

optionsAction
Action<IServiceProvider,DbContextOptionsBuilder>

コンテキストの DbContextOptions を構成するオプションのアクション。 これにより、派生コンテキストで OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドすることで、コンテキストの構成を実行する代わりに使用できます。

ここでアクションが指定されている場合、派生コンテキストでオーバーライドされた場合でも、 OnConfiguring(DbContextOptionsBuilder) メソッドは実行されます。 OnConfiguring(DbContextOptionsBuilder) 構成は、ここで実行される構成に加えて適用されます。

オプションをコンテキストに渡すには、 DbContextOptions<TContext> を受け取り、それを DbContextの基本コンストラクターに渡すコンストラクターをコンテキストで公開する必要があります。

lifetime
ServiceLifetime

ファクトリとオプションを登録する有効期間。 既定値は Singleton です

戻り値

複数の呼び出しをチェーンできるように、同じサービス コレクション。

注釈

コンテキスト型を直接登録する代わりにファクトリを登録すると、新しい DbContext インスタンスを簡単に作成できます。 Blazor アプリケーションやその他の状況で依存関係の挿入スコープがコンテキストの有効期間と一致しない場合は、ファクトリの登録をお勧めします。

Blazor など、アプリケーションで依存関係の挿入を使用する場合は、このメソッドを使用します。 依存関係の挿入を使用しないアプリケーションの場合は、コンストラクターを使用して DbContext インスタンスを直接作成することを検討してください。 その後、OnConfiguring(DbContextOptionsBuilder) メソッドをオーバーライドして、接続文字列やその他のオプションを構成できます。

便宜上、このメソッドはコンテキスト型自体をスコープサービスとして登録します。 これにより、必要に応じて、コンテキスト インスタンスを依存関係挿入スコープから直接解決したり、ファクトリによって作成したりできます。

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

Entity Framework Core では、同じ DbContext インスタンス上での複数の並列操作の実行がサポートされていません。 これには、非同期クエリの並列実行と、複数のスレッドからの明示的な同時使用の両方が含まれます。 したがって、常に非同期呼び出しをすぐに待機するか、並列で実行される操作に個別の DbContext インスタンスを使用します。 詳細と例については、 DbContext スレッドの問題の回避に 関するページを参照してください。

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

適用対象