EntityFrameworkServiceCollectionExtensions.AddDbContext 方法
定義
多載
AddDbContext<TContext>(IServiceCollection, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 當您在應用程式中使用相依性插入時(例如使用 ASP.NET),可以使用這個方法。 如需有關設定相依性插入的詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=526890 。 |
AddDbContext<TContext>(IServiceCollection, ServiceLifetime, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。 如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。 |
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 。
這個多載有 |
AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。 如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。 |
AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。 如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。
這個多載有 |
AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。 如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。 |
AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。 如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。 |
AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) |
在中,將指定的內容註冊為服務 IServiceCollection 。 在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。 如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。
這個多載有 |
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 。
在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。
如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://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, 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);
}
適用於
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 建立和維護它自己的 IServiceProvider
適用于內部 Entity Framework 服務。
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 。
在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。
如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://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, 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));
}
適用於
AddDbContext<TContext>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)
在中,將指定的內容註冊為服務 IServiceCollection 。
在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。
如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。
這個多載有 optionsAction
提供應用程式的 IServiceProvider 。 如果您想要設定 Entity Framework Core 從主要應用程式服務提供者解析其內部服務,這會很有用。
根據預設,我們建議使用 AddDbContext<TContext>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)
這可讓 Entity Framework 建立和維護本身 IServiceProvider 的內部 Entity Framework 服務。
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));
}
適用於
AddDbContext<TContextService,TContextImplementation>(IServiceCollection, ServiceLifetime, ServiceLifetime)
在中,將指定的內容註冊為服務 IServiceCollection 。
在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。
如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。
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);
}
適用於
AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)
在中,將指定的內容註冊為服務 IServiceCollection 。
在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。
如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。
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));
}
適用於
AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)
在中,將指定的內容註冊為服務 IServiceCollection 。
在您的應用程式中使用相依性插入(例如 ASP.NET Core)時,請使用這個方法。 針對不使用相依性插入的應用程式,請考慮直接使用它的函式建立 DbContext 實例。 OnConfiguring(DbContextOptionsBuilder)然後可以覆寫方法,以設定連接字串和其他選項。
如需有關如何使用這個方法的詳細資訊,請參閱中的 Entity Framework Core 檔 https://aka.ms/efdocs 。 如需使用相依性插入的詳細資訊,請參閱 https://go.microsoft.com/fwlink/?LinkId=526890 。
這個多載有 optionsAction
提供應用程式的 IServiceProvider 。 如果您想要設定 Entity Framework Core 從主要應用程式服務提供者解析其內部服務,這會很有用。
根據預設,我們建議使用 AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action<DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime)
這可讓 Entity Framework 建立和維護本身 IServiceProvider 的內部 Entity Framework 服務。
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));
}