2

重要:人點的常見原因之前,在連接字符串工作在一個項目中我的本地機器上,而不是其他項目上相同的本地機器連接到相同的本地實例到同一個數據庫。EF核心無法連接到服務器 - 網絡相關的或特定於實例的錯誤

我有一個奇怪的問題。我有2個EF核心項目。我在第一個項目中的連接字符串完美工作。我可以輸入「dotnet ef database update」,它會將我的遷移推送到LOCAL數據庫。我完全複製粘貼下面的字符串,我的第二個項目,它拋出的錯誤:

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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

這裏是我的連接字符串:

Server=.;Database=RAMSAPPDB;Integrated Security=False;Persist Security Info=True;MultipleActiveResultSets=True; 

爲什麼使用此連接字符串我想不出一個理由在我試圖添加到SAME數據庫的其他項目中,會引發此錯誤。在第二個項目中推送遷移之前,我嘗試刪除RAMSAPPDB中的所有表,但它不起作用。

如果我註釋掉「的ConnectionString」,我得到的錯誤

connectionString cannot be blank

所以我知道這是我的appsettings.json使用連接字符串。

任何想法可能的原因將不勝感激。謝謝。

+0

選中此項:http://stackoverflow.com/questions/9945409/how-doi-i-fix-the-error-named-pipes-provider-error-40-could-not-open-a-connec和這個: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/2a8f2e64-74cf-46f2-b2be-bbb08b1502cb/re-named-pipes-provider-error-40-could-not-open -a-connection-to-sql-server?forum = sqlreportingservices – Pawel

+0

我已經看到了一般答案,但問題是它在一個項目中完美運行,但不是其他項目。我錯過了什麼嗎? –

+0

愚蠢的提示...你可以調試和檢查在運行時使用哪個確切的連接字符串? –

回答

1

我解決了它。我有一個很久以前寫的這一點,忘記了

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
{ 

optionsBuilder.UseSqlServer("Server=.\\SQLEXPRESS;Database=MYDB;Integrated  Security=True;MultipleActiveResultSets=True;"); 

} 
在我的DbContext類

,當我跑了「DOTNET EF數據庫更新」命令被使用。我認爲這是使用我的appsettings.json中的內容。

+0

哦,我的天啊!有這個EXACT同樣的問題!對於EF Core Migrations來說,這是一件非常偷偷摸摸的事情,因爲它自動添加的連接字符串與我們在部署時使用的連接字符串不同(當然)。把我的腦袋打了幾個小時,直到我找到你的答案!謝謝。 – starmandeluxe

相關問題