官方 ASP.NET Core 教學課程沒有說的小地方 - Part 1


Posted by 恩格斯 on 2020-09-13

當依照以下教程練習時 :

(English) add a model to a Razor Pages app in ASP.NET Core

(中文) 在 ASP.NET Core 中將模型新增至 Razor 頁面應用程式

在 PMC 中,輸入下列命令:

Add-Migration InitialCreate

Update-Database

會出現以下的 Error Message

訊息中會告訴我們

A network-related or instance-specific error occurred while establishing a connection to SQL Server.
The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

表示程式連不到我們的 SQL Server,所以拋出錯誤

明明有安裝好 SQL Server,也有安裝 Local db,為什麼連不到呢?

找了一陣子原因後,原來是在 appsettings.json 的 Db connection 沒有將 SQL Server 連結字串設定到正確的設定值

依照教程的步驟產生的 Db connection 字串類似如下 :

需改為

然後在 PMC 中再執行一次
Update-Database

就可以看到執行成功的畫面

開啟 SQL Server 也能看到由程式(所謂的 Code First)建立的 DataBase


#ASP.NET #ASP.NET Core #C#







Related Posts

到底什麼是建構函式?  我不知道

到底什麼是建構函式? 我不知道

JS30 Day 22 筆記

JS30 Day 22 筆記

金魚都能懂的這個網頁畫面怎麼切 — 學習筆記01

金魚都能懂的這個網頁畫面怎麼切 — 學習筆記01


Comments