Entity Framework Core and SQL Server on Mac

Stefan Riedmann
5 min readApr 23, 2020
All of these technologies are used in this tutorial

You want to work with .NET Core and SQL Server on your Mac? Then this one is for you. But you can also do all of this in Windows or Linux if you prefer to have your database in Docker.

This tutorial is based on a post on my blog which I wrote more than 2 years ago. People are still consulting me about it, so it seems it´s as relevant as ever. I decided to re-work it, update it to the newest versions, and create a fully working solution on GitHub. It will be all done with VSCode.

.NET Core is awesome, not only because it´s open-source and platform-independent but also because Microsoft, by refactoring it from scratch, could leave behind a lot of relics and grown code from .NET Framework. Entity Framework Core is a great example because they could redo everything based on the experiences from the original Entity Framework.

However, creating a stack with SQL Server and EFCore on Mac is still not quite the same as doing it on Windows — especially because you have to run the database in a docker. But still, it´s very straight forward to do so, and I´ll guide you through it step-by-step. I’ll also publish the solution on GitHub so you can just clone and run it. And I´ll only use VSCode, so you don´t have to install the (still not 100% convincing) VisualStudio for Mac, neither the SQL Operations Studio.

At first, I´ll give you a short overview of this guide.
1. Setup SQL Server in docker and connect with VSCode
2. Create a command-line application with .NET Core and EFCore
3. Create a basic code-first data model and the migration
4. Run and test it

So let´s get started…

  1. Setup SQL Server in Docker and connect with VSCode

If you don’t have it yet, please download and install Docker for Mac.

Then you’ll need the docker image for SQL Server on Linux, which can be installed with this command:

docker pull mcr.microsoft.com/mssql/server

Then, you’ll have to create a container of this image:

docker run -e ‘ACCEPT_EULA=Y’ -e ‘SA_PASSWORD=1StrongPassword!’ -p 1433:1433 — name mssql1 -d mcr.microsoft.com/mssql/server

The Docker extension from Microsoft for VSCode is a nice tool to see images, containers, and more about your Docker environment. Make sure the container is really running (green symbol), and that there was no problem with the run command.

Visual Studio Code with Docker

Now I recommend you to make sure your database is available on the localhost. For this I’m using the SQL Server extension. Believe me, even if it seems so I don’t get paid by Microsoft for all that advertisement.

In the extension, just click the + button, and it’ll ask you for

Hostname: localhost
Database to connect: blank
Authentication Type: SQL Login
Username: sa
Password: 1StrongPassword!
Visual Studio Code with SQL Server

You could also use Docker to connect to the image, and run SQL commands to check it. But using this extension will come in handy soon.

2. Create a command-line application with .NET Core and install packages

Now it’s time to start with the application. If you don’t have .NET Core SDK installed yet, you can find it here.

You can either clone my repository directly and jump directly to point 3.

git clone https://github.com/StefanRiedmann/EFCoreBasic.git

Or you follow these steps to pull in the necessary packages.

dotnet new console -o EFCoreBasic
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

Now this is to get the design package for creating migrations.

dotnet tool install — global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design

3. Create a basic code-first data model and the migration

If you pulled the repository, there’s not much more to do than run the code (point 4). There you will execute the migration to create the database, tables, and relationships.

If not, please follow these steps.

Create classes for each table, each class should have an id following the Entity Framework conventions

Create the DbContext and a DbSet for each table (class) and overwrite the OnConfiguring method to provide the connection string.

These are my classes and the DbContext on GitHub.

Data models and DbContext for the garden database

Now the code is ready for creating a first migration. This will create the C# code to generate tables and relations in the SQL database once you execute it.

dotnet ef migrations add InitialCreate

4. Run and test it

By running this command, you can now create the database and execute the migration.

dotnet ef database update

Entity Framework is using the previously generated migrations of the application, checks them against the database, and executes the schema changes. Now you can see that the tables were created.

GardenDb1 with tables

You can run my application with the following command in order to create some sample data.

dotnet run CreateSampleData

If you run my example without the argument, the application will read all the crops of all gardens that are planted this year, using an ordered LINQ query through Entity Framework Core, and it prints them on the command line.

This is my Program.cs with the method to create data, and the method with the query.

Garden program that can generate sample data and query it from the database

Here you can see a screenshot of the LINQ query, together with the command line output.

Query and output on the command line.

Congratulations, you are all set. If there’s any problem, doubt or issue, please don’t hesitate to contact me!

HAPPY PLANTING

--

--