(C# ASP.NET Core) Connectivity to SQL Server Running in a Docker Container

In this tutorial we shall learn how to connect an ASP.NET Core application to a SQL Server image that is running inside a docker container. Some data is entered and later verified that the data indeed reached the SQL Server Instance.

Categories | About |     |  

Parveen,

Step 1 - Start the SQL Server Image Container

First of all we have to start the image of SQL Server in a docker container.

Open the Docker Desktop, and then the command prompt to run the container as explained in the previous tutorial.

It is a good practice to query the list of all containers. For that, type the docker container ls command. This command will give us the list of all containers that are currently running.


docker container ls

Hit "enter" to verify that our SQL is indeed running.


C:\Users\Home>docker container ls

CONTAINER ID   IMAGE                                        NAMES
e8f76e75eca8   mcr.microsoft.com/mssql/server:2019-latest   sql1

Video Explanation

Please watch the following youtube video:

Step 2 - Connect to the SQL Server Object Explorer

Next let us connect to our SQL Server through the SQL Server Object Explorer.

Open the hellodocker project and use the menu View > SQL Server Object Explorer to open the explorer window. This window shows all your SQL Server connections.

Click on add connection icon to open the connections window. Enter Server = localhost, 1433, Authentication = SQL Server Authentication, User Name = SA and the password as set in the docker command. Leave the database name as empty - we shall set it through the connection string later.

Click connect!

If everything goes fine, then you should see your connection in the explorer window.

Right click the connection entry and open the Properties window. Find connection string and copy it. We will need it in our razor project.

Step 3 - Configure HelloDocker for SQL Connectivity

Let us now configure our HelloDocker project for SQL Server EF Core Framework.

Use the Tools menu to reach Manage NuGet Packages.

Search and add the usual packages for Microsoft.EntityFrameworkCore.SqlServer.

Next open the Program.cs file and add dbcontext with the UseSqlServer method. Paste your connection string and also add the name of your database. The code first approach will create the database and its tables for us.

// minimal code shown Program.cs 
// see the attached downloads or the video 
using HelloDocker.Pages.Models;

using Microsoft.AspNetCore.Builder;

using Microsoft.AspNetCore.Hosting;

using Microsoft.EntityFrameworkCore;

using Microsoft.Extensions.DependencyInjection;

using Microsoft.Extensions.Hosting;

Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{

  webBuilder.ConfigureServices(services =>
  {

    services.AddDbContext<MyProjectContext>(

    // configure sql server 
    opt => opt.UseSqlServer(
    @"Data Source=localhost,1433;" +
    "User ID=SA;Password=qw123QE!@4;" +
    "Initial Catalog=mydb;Connect Timeout=30;" +
    "Encrypt=False;TrustServerCertificate=False;" +
    "ApplicationIntent=ReadWrite;MultiSubnetFailover=False")
    );


    // configure razor support 
    services.AddRazorPages();

  });

  webBuilder.Configure(app =>
  {

    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {

      endpoints.MapRazorPages();

    });

  });

})

.Build()
.Run();

Run the project.

Add some data to verify that the data is being added successfully.

Verify the Data

Finally, open the SQL Server Object Explorer to check that the database and the table was created by EF Core.

Open the table to have a look at the data. Thanks!


This Blog Post/Article "(C# ASP.NET Core) Connectivity to SQL Server Running in a Docker Container" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.