无法连接到ASP.NET Core 1 EF7上的SQL Server [英] Cannot connect to SQL Server on ASP.NET Core 1 EF7

查看:110
本文介绍了无法连接到ASP.NET Core 1 EF7上的SQL Server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从ASP.NET Core 1.0 Web应用程序(实体框架核心)连接到SQL Server 2014或2016。我收到下面的错误。任何解决该问题的技巧。



我可以通过Visual Studio 2015 SQL Server对象资源管理器成功连接到数据库。但是,来自Web应用程序的连接失败。我都无法执行 dotnet ef数据库更新



我可以连接到LOCALDB。



我的连接字符串如下:

  DefaultConnection:数据源=服务器;初始目录= TestDb;集成安全性= False;用户ID = sa;密码= *****;连接超时= 15;加密= False; TrustServerCertificate = True; ApplicationIntent = ReadWrite; MultiSubnetFailover = False 

我得到的错误是。我在SQL Server 2014和2016上遇到相同的错误:

 处理请求时发生未处理的异常。 

Win32Exception:找不到网络路径
.ctor

SqlException:建立与SQL Server的连接时发生了与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称正确,并且已将SQL Server配置为允许远程连接。 (提供者:命名管道提供程序,错误:40-无法打开与SQL Server的连接)

堆栈跟踪为:

  Win32Exception:找不到网络路径
.ctor
CreateConnection
CreatePooledConnection
CreateObject
UserCreateRequest
TryGetConnection
WaitForPendingOpen
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandNextNonSuccessAndDebuggerNotification b MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
MoveNextN $ S $$$ b ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebugge rNotification
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
MoveNextNonSuccess
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandNextNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonS
HandleNonSuccessAndDebuggerNotification
MoveNext
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext
ThrowForNonSuccess
HandleNonSuccessAndDebuggerNotification
MoveNext

编辑1



请注意,这与以下问题不同:为什么我会得到



任何想法这些项目之间有什么区别?

解决方案

我今天遇到了类似的问题。看来与SQL Server的.Net Core连接字符串与以前的.Net SQL Server连接字符串有些不同。您是否尝试过使用与在MSDN博客上指定的位置?

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

(如果滚动向下到关系数据库和 SQL Server子标题,您将找到示例。)



如果在字符串中使用用户名和密码,则表示也可以尝试以下操作:

 服务器= tcp:您的服务器名,1433;初始目录=您的数据库名;用户ID =您的用户名;密码=你的密码; 


I am trying to connect to SQL Server 2014 or 2016 from an ASP.NET Core 1.0 web application (Entity Framework Core). I am getting the error below. Any tips how to resolve it.

I can successfully connect to the DB from through Visual Studio 2015 SQL Server Object Explorer. However the connection from the web application fails. Neither I can do dotnet ef database update.

I can connect to LOCALDB.

My connection string looks like:

"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"

The error I get is. I get the same error on SQL Server 2014 and 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)

The stack trace is:

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

EDIT 1

Please note, this is not the same question as: Why am I getting "Cannot Connect to Server - A network-related or instance-specific error"?

EDIT 2

The type of this project where I have problem is ASP.NET Core Web Application (.NET Core).

However I have tried by creating ASP.NET Core Web Application (.NET Framework) project and there was no problem there. I was able to connect.

Any ideas what is different between these projects?

解决方案

I had a similar problem today. It seems that the .Net Core connection string to SQL Server is a little different from prior .Net SQL Server connection strings. Have you tried using something similar to the one specified here on the MSDN Blog?:

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

(If you scroll down the page to the 'Relational databases' and 'SQL Server' sub heading, you will find examples.)

If you are using username and password in the string, you could also try this:

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

这篇关于无法连接到ASP.NET Core 1 EF7上的SQL Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆