更新数据库:“在建立到SQL Server的连接时发生了与网络相关或特定于实例的错误". [英] update-database: "A network-related or instance-specific error occurred while establishing a connection to SQL Server"

查看:167
本文介绍了更新数据库:“在建立到SQL Server的连接时发生了与网络相关或特定于实例的错误".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的C#项目.这是我在web.config中用于数据库的连接字符串:

I have a simple C# project. This is my connection string in my web.config for the database:

<connectionStrings>
   <add name="DefaultConnection" 
        connectionString="Data Source=172.17.0.47;Initial Catalog=DevSystemListe;User ID=web_access;Password=123456" 
        providerName="System.Data.SqlClient" />
</connectionStrings>

我已经确保此连接正常工作.通过这种连接,我可以从Visual Studio连接到数据库,也可以看到表和数据.

I already made sure that this connection is working. I can connect to my database from the Visual Studio with this connection and I can also see the tables and data.

当我想用update-database更新数据库时,会发生此错误:

When I want to update my database with update-database, this error occurs:

建立与SQL Server的连接时发生与网络相关或特定于实例的错误.服务器未找到或无法访问.验证实例名称正确,并且已将SQL Server配置为允许远程连接. (提供者:SQL网络接口,错误:26-指定服务器/实例时出错)

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)

我卸载了Entity Framework,然后再次安装了它.什么都没改变.

I uninstalled Entity Framework and then installed it again. Nothing changed.

推荐答案

我终于找到了.我的解决方案中有一个网站项目和我的API.问题在于该网站被设置为启动项目.因此,该实体框架在网站的web.config中而不是在api的web.config中搜索连接字符串.将启动项目更改为我的API可以解决此问题.

I finally found out. I have a website project and my API in my solution. The problem was that the website was set as startup project. So that entity framework searched the connection string in the website's web.config and not in the api's web.config. Changing the startup project to my API solved the problem.

如果要将网站项目作为启动项目.只需将连接字符串复制到Websides web.config.

If you want to have the website project as startup project. Just copy the connection string into the websides web.config.

尽管如此,感谢大家的帮助.

Nevertheless thanks for everyones help.

这篇关于更新数据库:“在建立到SQL Server的连接时发生了与网络相关或特定于实例的错误".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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