asp.netsql-serverdockermacosazure-data-studio

Cannot connect my ASP.NET Web API to SQL Server on macOS (Docker) – "Cannot connect to SQL Server Browser"


Cannot connect my ASP.NET Web API to SQL Server on macOS (Docker) – "Cannot connect to SQL Server Browser"

Problem:

I am developing an ASP.NET Web API on VS Code (macOS) and trying to connect it to my SQL Server running in Docker. However, I keep getting the following error:

Cannot connect to SQL Server Browser. Ensure SQL Server Browser has been started.

My setup:

  1. Environment:

    • macOS
    • SQL Server running in Docker
    • ASP.NET Web API running in VS Code
    • Using Azure Data Studio to create and manage the database
  2. Connection string (appsettings.json):

    "ConnectionStrings": {
      "DefaultConnection": "Server=localhost,1433;Database=YourDB;User Id=sa;Password=****;TrustServerCertificate=True;"
    }
    
  3. What I've tried:

    • I verified that SQL Server is running by connecting via Azure Data Studio.
    • Ran lsof -i -P -n | grep LISTEN | grep 1433, and it shows that SQL Server is listening on port 1433.
    • Attempted to connect using sqlcmd -S localhost,1433 -U sa -P Str0ngP@ssword!, but I get:
      Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
      

Question:


Solution

  • I forgot about how I run the scaffold command on my backend, it had the instance name included on it (mentioned by @AlwaysLearning). I just had to run the scaffold command again with the correct values to fix it, which is this:

    dotnet ef dbcontext scaffold "Server=localhost,1433;Database=CanteenDB;User Id=sa;Password=******;TrustServerCertificate=True;" Microsoft.EntityFrameworkCore.SqlServer -o Entities --force