asd*_*psd 2 sql-server entity-framework-core docker-compose asp.net-core
It fails to connect to the SQL Server container, even if the backend is running in local and the database in docker.
I tested with Azure SQL and everything worked perfectly.
Error(s):
2021-07-13 21:43:32.79 Logon Login failed for user 'sa'. Reason: Failed to open the explicitly specified database 'AsdDB'. [CLIENT: XXX.168.XX.3]
backend | fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
backend | An error occurred using the connection to database 'AsdDB' on server 'sqlserver'.
backend | fail: Microsoft.EntityFrameworkCore.Query[10100]
backend | An exception occurred while iterating over the results of a query for context type 'Asd.Db.AppDbContext'.
backend | Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "AsdDB" requested by the login. The login failed.
backend | Login failed for user 'sa'.
Run Code Online (Sandbox Code Playgroud)
dotnet ef update databasedotnet ef database update --project Asd.Api.csproj
Build started...
Build succeeded.
Run Code Online (Sandbox Code Playgroud)
Microsoft.Data.SqlClient.SqlException (0x80131904): 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)
后端 Dockerfile:
FROM mcr.microsoft.com/dotnet/core/sdk:3.1-bionic AS build
WORKDIR /app
EXPOSE 80
COPY *.sln .
COPY Asd.Api/*.*.csproj ./Asd.Api/
COPY Asd.Db/*.*.csproj ./Asd.Db/
RUN dotnet restore
COPY Asd.Api/. ./Asd.Api/
COPY Asd.Db/. ./Asd.Db/
WORKDIR /app/Asd.Api
RUN dotnet publish -c Release -o out
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1-bionic AS runtime
WORKDIR /app
COPY --from=build /app/Asd.Api/out ./
ENTRYPOINT ["dotnet", "Asd.Api.dll"]
Run Code Online (Sandbox Code Playgroud)
Docker 组成:
version: "3.8"
services:
backend:
container_name: backend
build:
context: ./backend/
dockerfile: Dockerfile
depends_on:
- db
ports:
- "5000:80"
db:
image: "mcr.microsoft.com/mssql/server:2017-latest"
container_name: sqlserver
hostname: sqlserver
environment:
SA_PASSWORD: "ASDFGHJ12345"
ACCEPT_EULA: "Y"
restart: unless-stopped
ports:
- "1433:1433"
Run Code Online (Sandbox Code Playgroud)
appsettings.json 中的连接字符串
"ConnectionStrings": {
"DefaultConnection": "Server=sqlserver;Database=AsdDB;user id=sa;password=ASDFGHJ12345;"
},
Run Code Online (Sandbox Code Playgroud)
在连接字符串中,必须向服务器传递以下值:
服务器= host.docker.internal ;数据库=AsdDB;用户id=sa;密码=ASDFGHJ12345%;
所以现在它可以工作了,花了两天时间搞乱它,但在我发布问题后,我找到了答案。
| 归档时间: |
|
| 查看次数: |
1934 次 |
| 最近记录: |