5

I'm trying to finish the ASP.NET CORE tutorial on Pluralsight on a MAC. I'm running MSSQL server using Docker and its seems to work (i have the sql database up and running as shown here)

The second step was to have my asp.net core application to connect with this mssql database. Here are what i have for the connection string inside appsettings.json:

  "ConnectionStrings": {
    "OdeToFood2Db": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=OdeToFood2;Integrated Security=True"
  }

this is what i have for ConfigureServices() inside startup.cs:

services.AddDbContextPool<OdeToFood2DbContext>(
                options =>
                {
                    options.UseSqlServer(Configuration.GetConnectionString("OdeToFood2Db"));
                }
            );

I then tried to run

dotnet ef dbcontext info -s ../OdeToFood2/odeToFood2.csproj

but im getting the

Build started...
Build failed. Use dotnet build to see the errors.

error. I think the issue is that i have the connection string wrong since im running my mssql on docker and not locally like the tutorial i'm following.

If anyone could point me in the right direction that would definitely help a tons, i've been stuck on this issue for 5 days now and it is excruciating. Thanks in advance!

2 Answers 2

4
  "ConnectionStrings": {
    "MyWindowsConnection": "Server=(localdb)\\mssqllocaldb;Database=TestWinMac;Trusted_Connection=True;",
    "MyMacConnection": "Server=localhost,1433;Initial Catalog=TestWinMac;User ID=SA;Password=MyP@ssword"
  }

See my connection titled, "MyMacConnection". 1433 is the port. I have a project that exists on both Mac and Windows machines, and I switch out the connection string based on the platform I'm using.

Sign up to request clarification or add additional context in comments.

Comments

0

Docker runs its own internal virtual network on your machine, finding that IP/Hostname of your SQL container should help since technically your SQL instance is running inside a container.

On your docker machine run

docker ps

This should list all running containers, one of which should be your SQL container. The output should look similar to:

    CONTAINER ID   IMAGE          COMMAND                  CREATED      STATUS     PORTS        NAMES
    a8b6facb199c   SQL-Image     "some command"            X days ago   Up X days   8080        sqlserver

Of interest are the ports and name fields. Your ports field should list at least one port, this is likely the default of the SQL provider you're using but it may not be.

We now have the port to attempt to connect to, now we need the IP address. Run the following where is the value from the name field in the previous command (Or use the container ID)

docker inspect <image-name>

This will dump the attributes of your container including the IP address, this will likely be a 172.x IP address.

Now we have the port and the IP address you should be able to modify your connection string to point to those values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.