![](/img/trans.png)
[英]How to use dependency injection in ASP.NET Core 2.0 for IHttpcontextAccessor
[英]How to use Dependency Injection in .NET Core 2.0 for ADO.NET?
我正在使用.NET Core 2.0和ADO.NET,但遇到一個問題:如何為連接字符串使用DI?
在擁有DbContext之前(我注入了它),但現在沒有了,因為我不再使用EntityFramework(出於性能原因)。
我有2個基礎項目(層),根站點和核心項目。
根層僅負責MVC。 核心層具有我的業務和存儲庫邏輯。 我的存儲庫需要連接字符串才能通過ADO.NET訪問數據庫。
就像是:
using (SqlConnection conn = new SqlConnection(connectionString))
{
//...
}
我嘗試了一些可行的解決方案,例如使用帶有String的靜態類來存儲我的連接字符串,但我想知道最佳方法。
例:
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
DatabaseConnection.ConnectionString = Configuration["ConnectionStrings:DefaultConnection"];
//...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.