sql-server - 无法从 AspNetCore 应用程序连接 Docker MsSql

标签 sql-server docker asp.net-core

我正在尝试从我的 Aspnet Core 应用程序访问 Docker MsSql 实例。

当我尝试从 Management Studio 时,我可以连接到 MsSql 服务器。但是,当我尝试运行该应用程序时,收到以下错误消息。

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: TCP Provider, error: 40 - Could not open a connection to SQL Server)

这是我在 config.json 中使用的连接字符串

{ “连接字符串”:{ "MyConnectionString": "数据源=127.0.0.1;初始目录=测试;集成安全性=False;持久安全信息=True;用户ID=SA;密码=##someStrongPass~~;" } }

最佳答案

我找到了解决方案。

如果有人从容器运行其 AspNetCore 应用程序并使用第二个容器作为数据库实例。确保在连接字符串中使用主机 IP 并从数据库容器公开数据库端口。与Linux上的容器不同,Windows上的Docker容器无法直接连接到DB容器的IP地址。希望这个问题可以在未来的版本中得到解决。

所以在这种情况下,连接字符串应该是

{ “连接字符串”:{ "MyConnectionString": "数据源 = YOURMACHINEsIP;初始目录 = 测试;集成安全性 = False;持久安全信息 = True;用户 ID = SA;密码 = SomeStrongPass;" } }

关于sql-server - 无法从 AspNetCore 应用程序连接 Docker MsSql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54755400/

相关文章:

sql - 使用日期部分的Where子句中的案例

linux - docker "Unable to find group docker"使用 "--group-add docker"

redis - 我如何使用 phusion/baseimage-docker "dockerize"一个 redis 服务

c# - Asp.net core + EF Code优先,迁移不同项目中的文件

c# - 如何在 EF Core 3.1 中以异步方式使用 GroupBy?

c# - 将 IHttpClientFactory 传递给 .NET Standard 类库

sql - TSQL,帮助进行有关用户年龄的小查询

sql-server - 如何在 S3 中查询异构 JSON 数据?

sql-server - 在数据库名称上连接 sys.columns 和 sys.tables

Docker ps 授予权限问题