我有一個在.NET Framework 4.6.1中作爲項目運行的EntityFramework 6的DAL。適用於其他.NET Framework項目的解決方案。現在我要使用引用到我的ASP.NET Core WebApi(使用.NET Framework 4.6.1)的相同DAL項目,並且它不起作用。例外情況如下:如何從被引用的.NET Framework項目中的appsettings.json中獲取設置
在應用程序配置文件中找不到名爲'MyEntities'的連接字符串。
.NET核心項目不具有web.config文件,所以EF6我如何將設置從我的Core項目中的appsettings.json傳遞到引用的DAL項目?
我發現了一些例子,讓EF6在.NET Core項目(如https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6)中工作,但它並沒有幫助我。我想這是因爲dbcontext是在DAL項目中創建的。
appsettings.json
"ConnectionStrings": {
"MyEntities": "<my connectionString>"
},
project.json
"dependencies": {
"EntityFramework": "6.1.3",
...
},
...
"frameworks": {
"net461": {
"dependencies": {
"DAL": {
"target": "project"
}
},
"imports": "dnxcore50"
}
},
我不知道如何把這個在我的控制器。我應該如何初始化'Config',以及如何將它傳遞給DAL項目? –
我找到了一個[answer](http://stackoverflow.com/a/40902384),我可以使用它將連接字符串傳遞給項目。但這不是我的解決方案,因此DAL被其他項目使用,我無法改變處理來自他們的呼叫的方式。 我希望我可以讓我的appsettings.json令人放心。網絡解決方案中的項目。 –
使您的DAL項目接受連接字符串作爲靜態屬性或構造函數參數。然後從將使用DAL項目的項目傳遞連接字符串。 – Sriram