無法連接到ASP.NET Core 1 EF7上的SQL Server

asp.net-core asp.net-core-1.0 entity-framework-core sql-server

我試圖從ASP.NET Core 1.0 Web應用程序(實體框架核心)連接到SQL Server 2014或2016。我收到以下錯誤。任何提示如何解決它。

我可以通過Visual Studio 2015 SQL Server對象資源管理器成功連接到數據庫。但是,Web應用程序的連接失敗。我也不能做dotnet ef database update

我可以連接到LOCALDB。

我的連接字符串如下所示:

"DefaultConnection": "Data Source=server;Initial Catalog=TestDb;Integrated Security=False;User ID=sa;Password=*****;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

我得到的錯誤是。我在SQL Server 2014和2016上遇到了同樣的錯誤:

An unhandled exception occurred while processing the request.

Win32Exception: The network path was not found
.ctor

SqlException: 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)

堆棧跟踪是:

Win32Exception: The network path was not found
.ctor
CreateConnection
CreatePooledConnection
CreateObject
UserCreateRequest
TryGetConnection
WaitForPendingOpen
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
GetResult
MoveNext in AccountController.cs
                var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext

編輯1

請注意,這與以下問題不同:為什麼我會收到“無法連接到服務器 - 與網絡相關或特定於實例的錯誤”?

編輯2

我遇到問題的這個項目的類型是ASP.NET Core Web Application(.NET Core

但是,我嘗試創建ASP.NET核心Web應用程序(.NET Framework項目,那裡沒有問題。我能夠連接。 ASP.NET Core Web Application(.NET Core)不連接到SQL Server和.NET Framework)項目連接

任何想法這些項目之間有什麼不同?

熱門答案

我今天遇到了類似的問題。似乎SQL Server的.Net Core連接字符串與以前的.Net SQL Server連接字符串略有不同。您是否嘗試過使用類似於MSDN博客上指定內容的內容?

using (var connection = new SqlConnection("Server=tcp:YourServer,1433;Initial Catalog=YourDatabase;Persist Security Info=True;"))

(如果向下滾動頁面到“關係數據庫”和“SQL Server”子標題,您將找到示例。)

如果您在字符串中使用用戶名和密碼,您也可以嘗試這樣做:

"Server=tcp:yourservername,1433;Initial Catalog=yourdbname;User ID=yourusername;Password=yourpassword;"


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow