2016-07-28 40 views
1

我正在學習使用Visual Studio與教程構建ASP.NET Core MVC應用程序。在「Adding a model」的步驟我創建了一個新的單獨的項目(如寫入指令),但是當我運行dotnet ef database update,出現以下錯誤:dotnet ef數據庫更新中的網絡相關或特定於實例的錯誤

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: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

但我已經安裝了SQL Server和SQLEXPRESS服務正在運行。我也嘗試打開端口,但它沒有幫助。

+3

連接字符串是什麼? – DavidG

+0

你有這個工作嗎?我無法讓EF通過這個錯誤實際創建一個數據庫... – Joe

回答

4

設置的LocalDB

模板的連接字符串是localdb專門爲mssqllocaldb實例。確保你有兩個。

appsettings.json

{ 
    "ConnectionStrings": { 
    "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MvcMovie-87ad796f-548e-4862-9713-d2976453319e;Trusted_Connection=True;MultipleActiveResultSets=true" 
    } 
} 

確保已安裝的LocalDB。要檢查,請打開命令提示符並運行sqllocaldb。這將打印LocalDB命令行幫助信息。如果你沒有LocalDB,then install it.

確保你有一個名爲​​的實例。檢查,運行sqllocaldb info。如果您沒有該實例,則需要創建它(sqllocaldb create mssqllocaldb)或將連接字符串更改爲指向您擁有的實例。

工作在我的機器

dotnet ef database update我的機器上運行此命令的例子。以下是我的提示中的各種命令的輸出。

> sqllocaldb info 
MSSQLLocalDB 

> dotnet ef migrations add Initial 
Project MvcMovie (.NETCoreApp,Version=v1.0) will be compiled... 
Compiling MvcMovie for .NETCoreApp,Version=v1.0 
Compilation succeeded. 
    0 Warning(s) 
    0 Error(s) 
Time elapsed 00:00:01.7783361 

Done. To undo this action, use 'dotnet ef migrations remove' 

> dotnet ef database update 
Project MvcMovie (.NETCoreApp,Version=v1.0) was previously... 
Done. 
0

我想擴展Shaun Luttin的答案。我遇到了同樣的問題,但在這種情況下,我沒有安裝新的Visual Studio,它也給我帶來了問題。

這是我的解決方案(從現有的安裝):

  1. 轉到視圖,然後單擊「SQL Server的對象資源管理器」
  2. 在您的服務器列表中右鍵單擊服務器名,然後單擊「屬性」
  3. 查找「連接字符串」。

在這裏,在我的情況下,它說:

"Source=(localdb)\\ProjectsV13;Initial... 

略去了

所以,更換Server=(localdb)\\mssql(localdb)\\ProjectsV13

這應該做IT.1

enter image description here

相關問題