EntityFrameworkServiceCollectionExtensions.ConfigureDbContext 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
ConfigureDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime)
在 中配置給定的上下文類型。IServiceCollection
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureDbContext<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<IServiceProvider,Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member ConfigureDbContext : 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 ConfigureDbContext(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of IServiceProvider, DbContextOptionsBuilder), Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection
類型參數
- TContext
要登記的情境類型。
參數
- serviceCollection
- IServiceCollection
IServiceCollection新增服務。
- optionsAction
- Action<IServiceProvider,DbContextOptionsBuilder>
一個用來設定上下文 DbContextOptions 的動作。
- optionsLifetime
- ServiceLifetime
服務登記於貨櫃的存續期間 DbContextOptions 。
傳回
相同的服務集合,讓多通通話可以串聯起來。
備註
AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime), , AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32), AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32) 也必須被呼叫,指定配置才會生效。 在上述任一步驟後呼叫此方法,會重複寫出衝突的設定。 對於非池化上下文 OnConfiguring(DbContextOptionsBuilder) ,除了此處執行的配置外,也會套用配置。
此方法可多次呼叫,配置會依照指定順序套用。
更多資訊與範例請參見 使用 DbContext 與依賴注入 。
適用於
ConfigureDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)
在 中配置給定的上下文類型。IServiceCollection
public static Microsoft.Extensions.DependencyInjection.IServiceCollection ConfigureDbContext<TContext>(this Microsoft.Extensions.DependencyInjection.IServiceCollection serviceCollection, Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> optionsAction, Microsoft.Extensions.DependencyInjection.ServiceLifetime optionsLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) where TContext : Microsoft.EntityFrameworkCore.DbContext;
static member ConfigureDbContext : Microsoft.Extensions.DependencyInjection.IServiceCollection * Action<Microsoft.EntityFrameworkCore.DbContextOptionsBuilder> * Microsoft.Extensions.DependencyInjection.ServiceLifetime -> Microsoft.Extensions.DependencyInjection.IServiceCollection (requires 'Context :> Microsoft.EntityFrameworkCore.DbContext)
<Extension()>
Public Function ConfigureDbContext(Of TContext As DbContext) (serviceCollection As IServiceCollection, optionsAction As Action(Of DbContextOptionsBuilder), Optional optionsLifetime As ServiceLifetime = Microsoft.Extensions.DependencyInjection.ServiceLifetime.Singleton) As IServiceCollection
類型參數
- TContext
要登記的情境類型。
參數
- serviceCollection
- IServiceCollection
IServiceCollection新增服務。
- optionsAction
- Action<DbContextOptionsBuilder>
一個用來設定上下文 DbContextOptions 的動作。
- optionsLifetime
- ServiceLifetime
服務登記於貨櫃的存續期間 DbContextOptions 。
傳回
相同的服務集合,讓多通通話可以串聯起來。
備註
AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime), , AddDbContextPool<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32), AddDbContextFactory<TContext,TFactory>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime)AddPooledDbContextFactory<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, Int32) 也必須被呼叫,指定配置才會生效。 在上述任一步驟後呼叫此方法,會重複寫出衝突的設定。 對於非池化上下文 OnConfiguring(DbContextOptionsBuilder) ,除了此處執行的配置外,也會套用配置。
此方法可多次呼叫,配置會依照指定順序套用。
更多資訊與範例請參見 使用 DbContext 與依賴注入 。