c# - EF Core 无法连接到服务器 - 与网络相关或特定于实例的错误

标签 c# entity-framework asp.net-web-api asp.net-core asp.net-core-webapi

重要:在人们指出常见原因之前,连接字符串在我本地计算机上的一个项目中有效,但在连接到同一本地实例的同一本地计算机上的另一个项目中无效相同的数据库。

我有一个奇怪的问题。我有 2 个 EF 核心项目。我在第一个项目中的连接字符串工作得很好。我可以输入“dotnet ef database update”,它会将我的迁移推送到 LOCAL 数据库。我将下面的字符串完全复制粘贴到我的第二个项目中,它抛出了错误:

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)

这是我的连接字符串:

Server=.;Database=RAMSAPPDB;Integrated Security=False;Persist Security Info=True;MultipleActiveResultSets=True;

我想不出在我的其他项目中使用此连接字符串会抛出此错误的单一原因,该项目试图添加到 SAME 数据库。在第二个项目中推送迁移之前,我尝试删除 RAMSAPPDB 中的所有表,但它不起作用。

如果我注释掉“ConnectionString”,我会得到错误

connectionString cannot be blank

所以我知道它正在使用我的 appsettings.json 中的连接字符串。

如有任何关于可能原因的想法,我们将不胜感激。谢谢。

最佳答案

这个问题有时是由appsettings.json 文件在开发模式 下引起的。如果是这种情况,那么您应该查找 appsettings.Development.json 文件并在其中进行更改。

如果您在 Visual Studio 中并且启用了相关文件嵌套,则可以通过单击 appsettings.json 旁边的箭头访问 appsettings.Development.json,或者打开它并右键单击其标题栏,然后选择“打开包含文件夹”。 appsettings.Development.json 应该在那里,将连接字符串更改为您想要的内容,一切顺利。

关于c# - EF Core 无法连接到服务器 - 与网络相关或特定于实例的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42815746/

相关文章:

c# - 如何在数据库中保存一个特定值

entity-framework - net core odata put和patch嵌套数据结构

c# - 如何以编程方式隐藏桌面图标?

c# - 开始事务时出现 SqlServer.Management.SMO.Server 错误

c# - VB Collection - 转换为现代集合

c# - 在业务逻辑层使用 Entity Framework 生成的类

c# - 通知 WPF DataGrid 更改

c# - 如何保护 Windows 8 应用程序中的配置数据?

vue.js - 如何将 ASP Core Web API VueJS 站点部署到 IIS

.net - 变量名称模型绑定(bind)器中的 Web Api Dash