Getting Started ASP.NET Core using Visual Studio Code core
Microsoft had announced the general availability of .NET Core 2.0 along with ASP.NET Core 2.0 and Entity Framework 2.0 in August. The detailed blog post was available in .NET blog. I am really excited to try this out with my favorite lightweight x-platform editor Visual Studio Code. In this article, I will demonstrate how to create an API in ASP.NET Core 2.0 using Visual Studio Code.
The best way to learn is to build a simple API to perform a basic CRUD operation. It helps me to showcase how to use Visual Studio Code to develop ASP.NET Core application, writing unit test case, manage the version using Git and deploy the API to Azure. It is super easy to develop and deploy and I am sure you gonna like it!

Download Visual Studio Code and related tools

Visual Studio Code is a lightweight but powerful source code editor. It runs on your desktop and is available for Windows, macOS and Linux. It supports most of the languages using extensions. You can download the tool from here. Additionally Git is available to download the windows version fromĀ here. Once both the tools are downloaded, you can open Visual Studio Code and install following two extensions from the marketplace

Create a solution containing API and xUnit Test project

Once the development tools are ready to use, we will set up the solution containing two projects i.e; ASP.NET Core API project and xUnit test project. Please follow the below steps in order

  • Create a folder named “eKart” and open the folder in Visual Studio Code
  • Click on Git icon and initialize repository. This will initialize the local git repository
  • Open Visual Studio Code and click on terminal
  • Fire up the below commands in the order mentioned below

You can right click to add files to .gitignore. A new file named “.gitignore” creates to list the files to ignore. The suggestion is not to add folder [bin] and [obj] to git. Therefore please mention following lines to exclude in the .gitignore file

  • .\src\eKart.Api\bin\
  • .\src\eKart.Api\obj\
  • .\test\eKart.Test\bin\
  • .\test\eKart.Test\obj\
Prepare the API Project for Database Connectivity

The objective is to build the API using ASP.NET Core to perform CRUD operation and store the data in SQL database. Undoubtedly Entity Framework Core is the first class citizen as an ORM tool. This requires following nuget packages to add to the ASP.NET Core API project.

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Design
  • Microsoft.EntityFrameworkCore.Tools.DotNet

Perhaps the easiest way to add packages in Visual Studio Code is to use shortcut key [Ctrl + Shift + P] to open the command palette and type “nuget”. The following dialog appears and you can follow on-screen guidance to add the above packages. Once added, the package information is added to the csproj file.vs-code-nuget-mgr

Create the Model and DataSource Context

We will begin with a POCO class Product to map the Product entity.

Here a data context is required to have a two-way communication between the Product entity and the data source using Entity Framework Core. Apparently an interface is used to abstract out the implementation between SQL Server data context and in-memory data context and inject the specific implementation later.

Based on the above interface, the SQL Server implementation is derived likewise

Perform Entity Framework Core Migration

Entity Framework Core supports code-first approach to generate database objects. This requires a package “Microsoft.EntityFrameworkCore.Tools.DotNet” to install and we did it in the first step while setting up the ASP.NET Core API project. The migration will certainly fail unless the tool is referenced. Additionally database connection string is required in Startup.cs to initiate the migration.

Finally we can initiate the entity framework core migration to bring the changes in the database.

Create an API Controller

The objective of API Controller is to expose GET and POST endpoints to perform basic CRUD operation.

Run the API

Finally we are ready to run the API project. You can type the following commands to run the API