Skip to content

Instantly share code, notes, and snippets.

@jeremymaya
Last active April 12, 2024 11:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeremymaya/a36c1de8220d76beca85a2804a2cecc4 to your computer and use it in GitHub Desktop.
Save jeremymaya/a36c1de8220d76beca85a2804a2cecc4 to your computer and use it in GitHub Desktop.
ASP.NET Core Development with macOS

ASP.NET Core Development with macOS

This document was created to supplement the Code Fellows ASP.NET Core Curriculum for macOS users.

Available ASP.NET Core Developement Environment Options for macOS

Pros Cons
BootCamp
  • Native Windows 10 environment on macOS devices
  • Needs to split hard drive space
  • Drivers from BootCamp are not well optimized
  • Trackpad's multi-touch functionality is limited
  • Device runs hot and battery runs out fast
  • Shortcut keys are configured differently
  • Windows 10 needs to be purcahsed
Parallels/VMware Fusion
  • Virtualization offers more seamless and better experience than Windows 10 via BootCamp
  • Virual machine takes 30-40GB of hard drive
  • Possibilty of running into issues with folder/network setting that may take few hours to troubleshoot
  • Windows 10 and either Parallels or VMware Fusion need to be pruchased
  • Would not recommended if laptop's hardware spec is less than Quad Core, 16GB RAM and 258GB SSD
Visual Studio for macOS
  • Native IDE for macOS
  • Runs well on less powerful macOS devices
  • Free
  • Less developer firendly compare to Visual Studio 2019.
  • Supports .NET Core only

Install Visual Studio for Mac and .NET Core 3.1 SDK

  • Install Visual Studio for Mac here.
  • Install the .NET Core 3.1 SDK here.

SQL Server on macOS

Install the SQL Server on macOS using a Docker container

Follow these directions for installing the SQL Server on macOS.

  • For Step 5, the latest SQL Server container image can be checked here.
  • For Step 5, make sure to name your container with the --name sql_server_demo parameter in order to start and stop container from Terminal later.
  • Install Azure Data Studio for Mac here.
    • If you are having trouble opening the app from macOS Catalina, open the app by following these directions.

Migration and Update

This section of the guide supplements Default MVC Application.

Install .NET CLI

Since the Package Manager Console feature is not available on Visual Studio for Mac, we will use .NET Command-Line Interface (.NET CLI). .NET CLI is used to perform design-time development tasks such as migration or updates for a model based on an existing database.

  1. Install dotnet ef globally by entering the following into your Terminal:
    dotnet tool install --global dotnet-ef
  2. Install the latest Microsoft.EntityFrameworkCore.Design package by entering the following into your Terminal:
    dotnet add package Microsoft.EntityFrameworkCore.Design

Migration and Update with .NET CLI

  • Check if the server is connected. This can be checked by entering the following into your Terminal:
    Docker ps
  • If Docker server is not connected, start the server by entering the following into your Terminal:
    Docker start CONTAINERNAME
  1. Using Terminal, navigate to the project's root folder.

  2. Instead of the Connection String from Adding Entity Framework Core and a SQL Database Step 1, add the following Connection Strings to your appsettings.json file:

    "ConnectionStrings": {
     "DefaultConnection": "Server=localhost;Database=DBNAMEHERE;User=sa;Password=reallyStrongPwd123"
    }
  3. Instead of Add-Migration initial from Adding Entity Framework Core and a SQL Database Step 6, enter the following into your Terminal:

    dotnet ef migrations add initial

    Enter thr following into your Terminal when there are multiple databases connected:

    dotnet ef migrations add --context DBNAMEHERE initial
  4. Instead of Update-Database from Adding Entity Framework Core and a SQL Database Step 7, enter the following into your Terminal:

    dotnet ef database update

    Enter thr following into your Terminal when there are multiple databases connected:

    dotnet ef database update --context DBNAMEHERE
  5. Instead of SQL Server Object Explorer, the database can be checked with Azure Data Studio.

Remove Database

  1. To remove a database, enter the following into your Terminal:

    dotnet ef database drop -f

    Enter the following into your Terminal when there are multiple databases connected:

    dotnet ef database drop -f --context DBNAMEHERE

Entity Framework Scaffold for API Controller

This section of the guide supplements Lab: 12 - Intro to EFCore & APIs.

Follow the below steps to scaffold a controller using a DbContext class.

  1. Add required NuGet packages by entering the following into your Terminal:

    dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
    dotnet add package Microsoft.EntityFrameworkCore.Design
  2. Install the scaffolding engine by entering the following into your Terminal:

    dotnet tool install --global dotnet-aspnet-codegenerator
  3. Scaffold a Controller by entering the following into your Terminal:

    dotnet aspnet-codegenerator controller -name HotelsController -async -api -m Hotel -dc AsyncInnDbContext -outDir Controllers
    • -name is name of the controller
    • -m is model class to use
    • -dc is the DbContext class to use
    • outDir is the relative output folder path from project where the file are generated

    Above commands will create HotelsController in the Controller folder.


User Secrets

This section of the guide supplements How to add User Secrets to .NET Core through Visual Studio.

How to enable and access secret storage

Since the Manage User Secrets feature is not available on Visual Studio for Mac, we will need to add the secrets manually.

  1. If .microsoft folder has not been created, create a directory ~/.microsoft to store secrets by entering the following into your Terminal:
    mkdir ~/.microsoft
  2. Using Terminal, navigate to the project's root folder.
  3. Enable secret storage by entering the following into your Terminal:
    dotnet user-secrets init
  4. To directly access and update secrets.json file, press Command + Shift + . to see hidden files on Mac and navigate to ~/.microsoft/usersecrets/<user_secrets_id>/secrets.json.

Azure DevOps

This section of the guide supplements Azure DevOps Cheat Sheet - Create a Repo.

How to clone a repository from Azure DevOps

  1. Go to Repos tab and click Clone
  2. Clcik Generate Git Credential and save the generated password
  3. Copy the HTTPS command
  4. Enter the following into your Terminal to clone the repo:
    git clone repo HTTPS
  5. When prompted to enter the password, enter the saved password

Merge conflict with Azure DevOps

Follow these directions when there is a merge conflict.


Credit


Change Log

  • Scaffolding a API Controller Content Added - 29 Jul 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment