語言

EntityFrameworkServiceCollectionExtensions.AddPooledDbContextFactory 方法

定義

多載

名稱 Description
AddPooledDbContextFactory<TContext>(IServiceCollection, Int32)

在 中IServiceCollection暫IDbContextFactory<TContext>存一個池池,用於建立指定DbContext類型的實例,並使用自訂池大小。

AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)

在 中註冊 並IDbContextFactory<TContext>IServiceCollection建立特定DbContext類型的實例,實例會被集中以供重複使用。

AddPooledDbContextFactory<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)

在 中註冊 並IDbContextFactory<TContext>IServiceCollection建立特定DbContext類型的實例,實例會被集中以供重複使用。

AddPooledDbContextFactory<TContext>(IServiceCollection, Int32)

來源:
EntityFrameworkServiceCollectionExtensions.cs

在 中IServiceCollection暫IDbContextFactory<TContext>存一個池池,用於建立指定DbContext類型的實例,並使用自訂池大小。

public static Microsoft.Extensions.DependencyInjection.IServiceCollection AddPooledDbContextFactory<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, int poolSize = 1024) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member AddPooledDbContextFactory : Microsoft.Extensions.DependencyInjection.IServiceCollection * int -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, Optional poolSize As Integer = 1024) As IServiceCollection

類型參數

TContext

由工廠生產的類型 DbContext 。

參數

serviceCollection
IServiceCollection

IServiceCollection服務會被加入。

poolSize
Int32

池中保留的最大實例數 TContext 。 預設為1024。

傳回

同樣 IServiceCollection 地,這樣才能串接多個通話。

備註

此過載與 EF Core 集中配置模型 ConfigureDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)相符。 並允許指定自訂 poolSize。 透過設定 ConfigureDbContext<TContext> 的選項會自動使用,免除重複提供者設定的需求。

在應用程式中使用依賴注入時,例如使用 ASP.NET Core,請使用此方法。 對於不使用依賴注入的應用程式,可以考慮直接用其建構子建立 DbContext 實例。 接著可以覆寫該OnConfiguring(DbContextOptionsBuilder)方法來配置 連接字串 及其他選項。

Entity Framework Core 不支援在同一 DbContext 執行個體上執行多個平行作業。 這包括平行執行非同步查詢,以及明確同時使用多個執行緒。 因此,務必立即等待非同步呼叫,或使用獨立的 DbContext 實例來執行平行執行的操作。 更多資訊與範例請參見 「避免 DbContext 執行緒問題 」。

更多資訊與範例請參閱 《Using DbContext with dependency injection》、《 Using DbContext factories》及 《Using DbContext pooling 》。

適用於

AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32)

來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs

在 中註冊 並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 = 1024) 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 = 128) 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 = 1024) As IServiceCollection
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional poolSize As Integer = 128) 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 執行緒問題 」。

更多資訊與範例請參閱 《Using DbContext with dependency injection》、《 Using DbContext factories》及 《Using DbContext pooling 》。

適用於

AddPooledDbContextFactory<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, Int32)

來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs
來源:
EntityFrameworkServiceCollectionExtensions.cs

在 中註冊 並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 = 1024) 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 = 128) 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 = 1024) As IServiceCollection
<Extension()>
Public Function AddPooledDbContextFactory(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional poolSize As Integer = 128) 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 執行緒問題 」。

更多資訊與範例請參閱 《Using DbContext with dependency injection》、《 Using DbContext factories》及 《Using DbContext pooling 》。

適用於