【发布时间】:2021-01-02 17:41:03
【问题描述】:
我正在尝试通过另一个使用 .NET Core 3.1 构建的容器化应用程序(微服务)连接到 Docker 映像 SQL Server 数据库,但我收到 Docker 引发的以下错误:
fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
An error occurred using the connection to database 'ProductDb' on server 'localhost,1433'.
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'Infrastructure.ProductContext'.
Microsoft.Data.SqlClient.SqlException (0x80131904): 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)
我使用的连接字符串是:
"ProductDb": "Server=localhost,1433;Database=ProductDb;User Id=sa;Password=********;"
我需要提到的是,当它托管在 IIS 服务器上时,同样的应用程序可以正常工作。
【问题讨论】:
-
如果将
localhost,1433替换为.,1433会发生什么? 1433 甚至是必要的,因为它是 SQL Server 的默认端口。
标签: sql-server docker .net-core