Getting Started with .NET Core 5 on MacOS

Ricardo Griffith
7 min readFeb 26, 2021

You will need the latest Visual Studio 2019 for Mac (works great on Apple’s Big Sur MacOS) for coding, source control to secure our code, and a database to persist user input. My two recommendations are Git and Github for source control and remote repository. In general, both of these are used widely in the industry with great success. We will use PostgreSQL 12 for our data storage.

NOTE BEFORE YOU CONTINUE READING The intent of this article is to document the setup of my development Ubuntu 20.04 LTS virtual desktop. If you follow any of the provided instructions or advice, you do so at your own risk. Any opinions expressed are my own and not the views of my employer.

Let’s get started building a simple ASP.NET Core site based on .NET 5. Launch Visual Studio for Mac to get started. You should be greeted with a getting started dialog:

Click the + New button on the right of the intial dialog to begin a new project.

Choose Web Application (Model-View-Controller) under the ASP.NET Core section of the New Project dialog:

Click the Next button to continue.

At this point, you need to ensure to target the right framework. Select .NET 5 from the Target Framework dropdown box and No authentication is selected in the Authentication dropdown box:

Click the Next button to continue.

Name the project whatever you want, I went with the following:

A good practice to centrally store code in one place on your computer, typicallya repos directory. We definitely want to use source control, so confirm the Use git for version control and Create a .gitignore file to ignore inessential files. are both checked (see image above).

Next, click theCreate button to let the wizard create the solution on your local machine.

Right-click on the Dependencies folder then select Manage NuGet Packages... from the context menu that appears:

Use the search to find the packages below and add each of the dependencies to your project.

  • Microsoft.EntityFrameworkCore
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Relational
  • Microsoft.EntityFrameworkCore.Tools
  • Npgsql.EntityFrameworkCore.PostgreSQL

Once you have selected all the packages listed above, click theAccept button to install the selected packages. You will need to read and agree to any licensing terms and conditions. If you accept the agreements, you can continue to follow along with this article.

You can verify all NuGet packages were added to the project by expanding the Dependencies -> NuGet folder:

Let’s develop our application using Code First approach since Microsoft has put some solid effort into developing this area in .NET 5.

Enter Employer for the name of the class then click New.

Added two properties to the class:

using System;
namespace EmployeePortal.Models
{
public class Employer
{
public Int32 Id { get; set; }
public String CompanyName { get; set; }

}
}

Follow the same steps again to add another class:

using System;
namespace EmployeePortal.Models
{
public class Employee
{
public Int32 Id { get; set; }
public String FirstName { get; set; }
public String LastName { get; set; }

}
}

Finally, add a Empty Class and name it accordingly postfixed with ‘DBContext’ e.g., EmployerDBContext:

using System;
using Microsoft.EntityFrameworkCore;
namespace EmployeePortal.Models
{
public class EmployerDBContext : DbContext
{
public DbSet<Employer> Employers { get; set; }
public DbSet<Employee> Employees { get; set; }
public EmployerDBContext(DbContextOptions<EmployerDBContext> options) : base(options)
{
}
}
}

Add connection string to the appsettings configuration file:

{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"DefaultConnection": "Server=127.0.0.1;Port=5432;Database=EmployerPortal;User Id=postgres;Password=<YOUR-secure-PASSWORD-here>;"
}
}

Replace the bolded text above with the correct information to connect to your instance of PostgreSQL.

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<EmployerDBContext>(options =>
options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
services.AddControllersWithViews();
}

Now this is to get the design package for creating migrations. Enter the following into Terminal (choose View -> Terminal) and ensure you are inside of the project folder:

Type the following into Terminal:

$ dotnet tool install --global dotnet-ef

Next we need to add the migrations to the project (.NET will do all the heavy lifting — scripts are generated, you will notice a new folder in the project named Migrations):

$ dotnet ef migrations add InitialCreation

After the migration has been added, execute the fololowing command to do the actual database work (the system will take the scripts generated from the previous step and modify the database):

$ dotnet ef database update

I prefer a GUI like DataGrip for accessing PostgreSQL. DataGrip makes me more productive when working with PostgreSQL. There are few free options , one I would suggest that is really coder-centric is Microsoft Azure Data Studio. You will need the Microsoft PostgreSQL extension to work with PostgreSQL in this interface.

Refresh the entity tree to see the newly created objects.

In order to immediate see results, I recommend a quick SQL script to insert a few records in the database. See the script below (completely optional).

INSERT INTO "Employers"
("CompanyName")
VALUES
('Microsoft'),
('Apple'),
('Oracle'),
('IBM');

We will need a Controller (Model-View-Controller) to work with the data source and send output to the View (webpages). As I discovered writing this article, there is one prerequisite in order to use the Scaffolding feature in Visual Studio 2019 for Mac. Type the following command in a Terminal window:

$ dotnet tool install --global dotnet-aspnet-codegenerator

Right-click on the Controller folder select Add -> New Scaffolding… from the context menu.

The Add New Scaffolding dialog will appear. Select the MVC Controller with Views, using Entity Framework.

Fill out the dialog selecting the Employer Model class, the EmployerDBContext and naming the controller EmployerController.

Click the Finish button to add the new file.

I ran into a small issue with adding a new scafolding item to the project. Notice the red text in the last screenshot shows that the dotnet-aspnet-codegenerator. is not installed despite being successfully earlier. After a bit of research and trial-and-error, it would appear that the tool does not like being run against a path with spaces.

Moving the entire project to a new location without any spaces along the path allowed me to successfully Add the Scafolded EmployerController item.

Now is a great time to compile the application. Navigate to the Employer listing by adding /Employer to the URL: e.g., https://localhost:5001/Employer

Because we scaffolded the Employer entity, .NET graciously generated Create-Read-Update-Delete (CRUD) views and the associated actions in the controller. This means you can add new Employer records by clicking the Create New link and for each record displayed on this page, you can view, edit or delete. I recommended performing each function to see that Visual Studio has assisted you with working with the data almost immediately.

You should walk through the code generated by the controller to see how you can work with the data that is sent from each web form and how the data is sent back to the View.

I had originally planned to walk you through a bit more of the code, but I think this is a good stopping point. The most compelling feature of this new framework is the speed. Many people have written about the potential speed gains, I wouldn’t mind writing a comparison article in the near future.

Stay tuned, I’m considering a few articles for a deeper dive into using .NET 5.

Thanks for reading!

--

--

I am a passionate technology leader, entrepreneur, husband, and father who loves to help others through collaboration, writing, and mentoring.