EntityFrameworkServiceCollectionExtensions.AddPooledDbContextFactory 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)
IDbContextFactory<TContext>在 中 IServiceCollection 註冊 ,以建立指定 DbContext 型別的實例,其中實例會集區以供重複使用。
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 128) where TContext : Microsoft.EntityFrameworkCore.DbContext;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddPooledDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 1024) As IServiceCollection
類型參數
- TContext
要由處理站建立的 DbContext 型別。
參數
- serviceCollection
- IServiceCollection
要新增服務的 IServiceCollection。
- optionsAction
- Action<DbContextOptionsBuilder>
設定 DbContextOptions 內容的必要動作。 使用內容共用時,必須在外部執行選項群組態; OnConfiguring(DbContextOptionsBuilder) 不會呼叫 。
- poolSize
- Int32
設定集區保留的實例數目上限。 預設值為 1024。
傳回
相同的服務集合,以便鏈結多個呼叫。
備註
註冊處理站而不是直接註冊內容類型,可讓您輕鬆建立新的 DbContext 實例。 針對 Blazor 應用程式,以及相依性插入範圍與內容存留期不一致的其他情況,建議註冊處理站。
在應用程式中使用相依性插入時,請使用此方法,例如搭配 Blazor 使用。 對於不使用相依性插入的應用程式,請考慮直接使用其建構函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)接著可以覆寫 方法,以設定連接字串和其他選項。
Entity Framework Core 不支援在同一 DbContext 執行個體上執行多個平行作業。 這包括平行執行非同步查詢,以及明確同時使用多個執行緒。 因此,請一律立即等候非同步呼叫,或針對平行執行的作業使用個別的 DbCoNtext 實例。 如需詳細資訊和範例 ,請參閱避免 DbCoNtext 執行緒問題 。
如需詳細資訊和範例,請參閱 使用 DbCoNtext 搭配相依性插入、 使用 DbCoNtext Factory和 使用 DbCoNtext 共用 。
適用於
AddPooledDbContextFactory<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)
IDbContextFactory<TContext>在 中 IServiceCollection 註冊 ,以建立指定 DbContext 型別的實例,其中實例會集區以供重複使用。
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 128) where TContext : Microsoft.EntityFrameworkCore.DbContext;
public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddPooledDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<IServiceProvider, Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 128) As IServiceCollection
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 1024) As IServiceCollection
類型參數
- TContext
要由處理站建立的 DbContext 型別。
參數
- serviceCollection
- IServiceCollection
要新增服務的 IServiceCollection。
- optionsAction
- Action<IServiceProvider,DbContextOptionsBuilder>
設定 DbContextOptions 內容的必要動作。 使用內容共用時,必須在外部執行選項群組態; OnConfiguring(DbContextOptionsBuilder) 不會呼叫 。
- poolSize
- Int32
設定集區保留的實例數目上限。 預設值為 1024。
傳回
相同的服務集合,以便鏈結多個呼叫。
備註
註冊處理站而不是直接註冊內容類型,可讓您輕鬆建立新的 DbContext 實例。 針對 Blazor 應用程式,以及相依性插入範圍與內容存留期不一致的其他情況,建議註冊處理站。
在應用程式中使用相依性插入時,請使用此方法,例如搭配 Blazor 使用。 對於不使用相依性插入的應用程式,請考慮直接使用其建構函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)接著可以覆寫 方法,以設定連接字串和其他選項。
Entity Framework Core 不支援在同一 DbContext 執行個體上執行多個平行作業。 這包括平行執行非同步查詢,以及明確同時使用多個執行緒。 因此,請一律立即等候非同步呼叫,或針對平行執行的作業使用個別的 DbCoNtext 實例。 如需詳細資訊和範例 ,請參閱避免 DbCoNtext 執行緒問題 。
如需詳細資訊和範例,請參閱 使用 DbCoNtext 搭配相依性插入、 使用 DbCoNtext Factory和 使用 DbCoNtext 共用 。