Since the release of ASP.NET Core and the .Net Core framework, Microsoft has been pushing the idea of Cross Platform .Net development. When setting up projects, you generally have the option to connect to a database in the Cloud such as Microsoft Azure, SQL Server or a host of 3rd party DBs such as SQL Server and Postgres. Azure is great but sometimes you want to run a DB locally. This tutorial is here to show you how to run SQL Server on your Mac and connect it to your ASP.NET Core application.

One of the first steps you’ll need to do is have Docker running in your application. You can find it here (https://www.docker.com/docker-mac). Once you have it installed you should be able to run Docker commands from your terminal.

First let’s start off by downloading our SQL Server container.

docker pull microsoft/mssql-server-linux

This will pull in MS SQL Server. Then we have to install it.

docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=<SAPassWORD>' -p 1433:1433 -d microsoft/mssql-server-linux

You can now view that the instance is installed by running.

docker ps -a

This will show you a list of docker images. What is important is that you view the ID. Run the container and then log in using the ID. The image will automatically start but just in case you stop it and decide to go back run

docker start <ContainerID>.

Run docker ps –a again and you’ll see that the status is now up. Once it has started, try connecting to your Database. I’m currently using DataGrip.I create a new connection at localhost port 1433 with the same username sa and password. Create a new database called DockerTestSQL

Data Grip Docker Setup

Data Grip Docker Setup

Once that works let get started connecting it to ASP.NET Core.

Assuming that you already have .Net Core installed on  your mac,  create a new project from the command line. I’m using .Net core 2.0.

mkdir ProjectName && cd ProjectName

Then to create the project with authentication run

dotnet new mvc -au Individual -f netcoreapp2.0

Open the project in your ide. Inside of the appsettings.json file add a default connection string

"ConnectionStrings": {
  "DefaultConnection": "Data Source=127.0.0.1,1433;Initial Catalog=School;User ID=sa;Password=William22!;"
}

Then go to your SQL lite connection and change it to use Sql Server.

services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

To make sure this works, create your first migrations.

dotnet ef migrations add InitialCreate

You should now see your migrations in your Data/Migrations project directory. After that you can run

dotnet ef database update

This will push all of the schema changes to your DB. Run your application and create an example user. You should be automatically logged in. Go to your database and check that the user was created in the ASPNetUserRoles table

View User in SQL Server Docker Image

View User in SQL Server Docker Image

Conclusion

Being able to run SQL-Server in docker expands the number of environments that you can test you ASP.Net core app. For developers that use Linux and Mac, having a local environment to test before pushing to a cloud service helps to break free of always having to use a Windows machine but also allows teammates to have similar setups using docker.

Codebrains Newsletter

Get weekly dev news and tutorials.

Powered by ConvertKit