c# - 更新数据库 : "A network-related or instance-specific error occurred while establishing a connection to SQL Server"

标签 c# entity-framework visual-studio

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

<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 连接到我的数据库,我还可以看到表和数据。

当我想用 update-database 更新我的数据库时,出现了这个错误:

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,然后重新安装了它。什么都没有改变。

最佳答案

我终于知道了。我的解决方案中有一个网站项目和我的 API。问题是该网站被设置为启动项目。因此 Entity Framework 在网站的 web.config 中搜索连接字符串,而不是在 api 的 web.config 中搜索。将启动项目更改为我的 API 解决了问题。

如果你想将网站项目作为启动项目。只需将连接字符串复制到网站 web.config 即可。

不过还是感谢大家的帮助。

关于c# - 更新数据库 : "A network-related or instance-specific error occurred while establishing a connection to SQL Server",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264575/

相关文章:

c# - 返回加入 Linq C#

c# - 异步/等待问题 - 等待的操作结束会发生什么?

c# - c#中的行位置

c# - 如何测试使用 DbEntityValidationException 的异常扩展方法?

c# - 创建 LINQ to Entities 可以转换的属性

c# - 如何在 OrientDB .Net API 中执行 Massiveinsert?

c# - 如何在 ADO.NET Entity Framework 上执行 "select count(*)"

.net - Windows 安装程序出错 ... "Unable to get installer types"

visual-studio - 自定义 Visual Studio 的 Intellisense

c++ - Visual Studio 2013 将 EXEC 前缀添加到 makefile 输出