簡體   English   中英

ASP.Net無法從ISS看到我的數據庫

[英]ASP.Net can't see my database from the ISS

我決定學習如何制作一個簡單的ASP.Net項目,並通過“存儲庫模式”對數據庫項目進行引用

我的控制器要求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."

因此,為清楚起見,讓我概述解決方案的結構:

  • aspProj
    • 控制器
    • 觀看次數
    • 服務
    • App.config(1)
    • Web.config
    • ...
  • 數據庫
    • 實體
    • 儲存庫
    • App.config(2)
    • ...
  • 數據庫測試
    • Test.cs
    • App.config(3)
    • ...

我已經將以下connectionString添加到所有App.configWeb.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-part想要訪問它時,就沒有愛情了。

我以為可能是不知道從何而來的國際空間站...

有什么想法嗎?

__

編輯:

我的Web.config: 在此處輸入圖片說明

我的AppSetting.json: 在此處輸入圖片說明

好了,問題很明顯-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>

只需在Web.config文件中的<connectionStrings>下為MyDatabaseConnection添加一個新元素,它就可以工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM