2016-05-29 50 views
0

我決定學習如何通過存儲庫模式將一個簡單的ASP.Net項目與參考關聯到數據庫項目。ASP.Net無法從ISS中看到我的數據庫

我有我的控制器呼籲List<Weight>處理:

public IActionResult MyWeight() 
{ 
    var repo = new Database.Repositories.WeightRepository(); 
    var data = repo.GetWeight().Result; 

    return View(data); 
} 

repo.GetWeight()被調用時,我得到一個AggregateException錯誤,與內部異常說:

"No connection string named 'MyDatabaseConnection' could be found in the application config file." 

所以爲了清楚起見,讓我概述解決方案的結構:

  • aspP ROJ
    • 控制器
    • 查看
    • 服務
    • 的App.config(1)
    • 的Web.config
    • ...
  • 數據庫
    • 實體
    • App.config中(2)
    • ...
  • Database.Test
    • test.cs中
    • App.config中(3)
    • ...

我已經添加了以下connectionString所有App.config S和Web.config

<connectionStrings> 
    <add 
     name="MyDatabaseConnection" 
     connectionString="Data Source=(localdb)\MSSQLLocalDB;Integrated Security=True" 
     providerName="System.Data.SqlClient"/> 
</connectionStrings> 

我已經從從Visual Studio的Server Explorer,並通過從測試項目Test.cs文件測試數據庫。我可以插入數據和檢索沒有問題。 但是,當ASP.Net部分想要訪問它時,沒有愛。

我想這可能是國際空間站這不知道從那裏的路徑...

有什麼想法?

__

編輯:

我的web.config: enter image description here

我AppSetting。JSON: enter image description here

+0

該asp.net項目只需要一個'web.config'文件。連接字符串將進入Web配置文件的部分。 – Nkosi

+0

就像我在**編輯**中所做的一樣? –

+0

你的asp項目是什麼版本?我注意到你的應用程序設置爲'JSON' – Nkosi

回答

1

那麼問題是明確的 - ASP.NET嘗試使用一個連接字符串名爲MyDatabaseConnection訪問數據庫:

<connectionStrings> 
    <add name="MyDatabaseConnection" connectionString="put the connection to the db here..." /> 
    </connectionStrings> 

而且在你的web.config你只有名爲WeightDatabaseConnection連接字符串:

<connectionStrings> 
    <add name="WeightDatabaseConnection" connectionString="put the connection to the db here..." /> 
    </connectionStrings> 

只需添加一個新的元素MyDat在<connectionStrings>下的<connectionStrings>Web.config文件,它應該工作