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

查看:35
本文介绍了更新数据库:“与 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)

我卸载了实体框架,然后重新安装了它.什么都没有改变.

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.

还是感谢大家的帮助.

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

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