EntityFrameworkServiceCollectionExtensions.AddDbContext 方法

定義

多載

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.

此多載具有 提供 optionsAction 應用程式的 IServiceProvider 。 如果您想要設定 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)

在 中 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.

此多載具有 提供 optionsAction 應用程式的 IServiceProvider 。 如果您想要設定 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)

在 中 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)

在 中 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 執行緒問題

Entity Framework Core 不支援在同一 DbContext 執行個體上執行多個平行作業。 這包括平行執行非同步查詢,以及明確同時使用多個執行緒。 因此,請一律立即等候非同步呼叫,或針對平行執行的作業使用個別的 DbCoNtext 實例。 如需詳細資訊和範例,請參閱 避免 DbCoNtext 執行緒問題

如需詳細資訊和範例,請參閱 搭配相依性插入使用 DbCoNtext

這個多載具有 , optionsAction 可提供應用程式的 IServiceProvider 。 如果您想要設定 Entity Framework Core,以從主要應用程式服務提供者解析其內部服務,這會很有用。 根據預設,我們建議使用 AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) ,讓 Entity Framework 針對內部 Entity Framework 服務建立和維護自己的 IServiceProvider 實體架構。

適用於

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

在 中 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)

在 中 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)

在 中 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

這個多載具有 , optionsAction 可提供應用程式的 IServiceProvider 。 如果您想要設定 Entity Framework Core,以從主要應用程式服務提供者解析其內部服務,這會很有用。 根據預設,我們建議使用 AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) ,讓 Entity Framework 針對內部 Entity Framework 服務建立和維護自己的 IServiceProvider 實體架構。

適用於