If you are like me running SQL Server on Linux in a Docker container, you might be wondering how to connect it to your .NET Core application. Here’s how:

  1. Make sure that Docker is running by typing sudo systemctl status docker and check to make sure that it is active. If it is inactive, type sudo systemctl start docker.

  2. Make sure that SQL Server is running by typing docker ps -a and check the status column to see whether it is running or not. You can start it by typing sudo docker start CONTAINER ID where CONTAINER ID is the ID of the container listed in docker ps -a.

  3. In the appsettings.Development.json add the following:

"ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Database=MyDatabase;Trusted_Connection=True;User Id=MyUserId;Password=MyPassword"
}
  1. In Startup.cs type the following in the ConfigureServices method:
            services.AddDbContext<YourDbContext>(options =>
            {
                 options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            });
  1. Save the changes in the file.

  2. Run your application. Your application should now be able to connect to the database.