CRUD using .Net Core, Angular2, WebAPI

In Our previous article we have seen how to startup with .Net Core. In this article we will take a look on database operation according to previous sample application based previous concept.

If you are new to .Net Core, Please read previous post about .Net Core Startup

In this article we are going to explore,

  1. Create Database
  2. Use Entity Framework Core (Db First Approach),
    1. Overview EF Core
    2. Install Entity Framework
    3. Create Models
    4. Configure EF Service
  3. Use MVC 6
    1. Overview MVC6
    2. Use WebAPI
  4. Use AngularJS2
    1. Component,
    2. Route
    3. Service
  5. Configure Server
    1. Run App inside IIS
    2. Run App outside IIS

Let’s get started with step by step:

Create Database

Before we get started with IDE lets create a new database using SSMS2014. Name it as PhoneBook.


Create a table Named Contacts, copy & run the below script in SSMS2014

Let’s get started, following our previous topic I am going to use previous sample application, open it with Visual Studio 2015.


It will automatically started restoring the dependencies. Build & run it. The application is working perfectly.

Install Entity Framework: Before install let’s have an overview on EF Core new Features:

  • Modelling: This includes Basic modelling, Data annotations, Relationships and much more.
  • Change Tracking: This includes Accessing tracked state, Snapshot, Notification change tracking.
  • SaveChanges: This includes Basic save functionality,Async SaveChanges,Transactions.
  • Query: This includes Basic LINQ support, Async query, Raw SQL queries
  • Database schema management: This includes Database creation/deletion APIs, Relational database migrations, and Reverse engineer from database.
  • Database providers: This includes EntityFramework.SqlServer, Sqlite, InMemory
  • Platforms: Supports Universal Windows Platform (UWP), .NET Core, Full .NET

Get more details about EF Core.

Let’s add folders for Entity models in our sample app solution.


DbEntities: for model entities.

The installation of EF is pretty much simple. Open project.json file, point tools section modify the section with below line.


Save changes after modification.


Packages will automatically restored. Let’s get explanation what are those.

EntityFrameworkCore.SqlServer: Database Provider, that allows Entity Framework Core to be used with Microsoft SQL Server.

EntityFrameworkCore.Tools: Command line tool for EF Core. Includes Commands For Package Manager Console:

  • Scaffold-DbContext,
  • Add-Migration,
  • Udate-Database

For Command Window:

  • dotnet ef dbcontext scaffold

We will see how to use both command.

EntityFrameworkCore.SqlServer.Design: Design-time, that allows Entity Framework Core functionality (EF Core Migration) to be used with Microsoft SQL Server.

To access the Command line tools we need to add EntityFrameworkCore.Tools in tools section of our project.json.


Save changes after modification.

Command in Package Manager Console: Open package manager console.


Input below command then hit enter,

Scaffold-DbContext “Server=DESKTOP-4T79RA1;Database=PhoneBook;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models/DbEntities



Command in Command Window: Open Command Window navigate to project directory, type

D:\Article\ASP-CORE\CRUD\CoreMVCAngular2\src\CoreMVCAngular>dotnet ef –help

Here a list of option will be shown in command window, we are going to use dbcontext in Commands.


Next Input below command then hit enter,

D:\Article\ASP-CORE\CRUD\CoreMVCAngular2\src\CoreMVCAngular>dotnet ef dbcontext scaffold “Server=DESKTOP-4T79RA1;Database=PhoneBook;Trusted_Connection=True;” Microsoft.EntityFrameworkCore.SqlServer –output-dir Models/CwEntities


Here is a screen shot of both process that execute & generate models. We will keep DbEntities folder to work with & will delete the other folder.


 Configure EF Service:

In PhoneBookContext Class add constructor

In Startup class we need to enable EF services providing the connectionstring to

We have configure the EF services in our application, next we will work with MVC6 that is included in ASP.NET Core.

MVC 6: We have already discuss about MVC6 in our previous post, let’s have an overview on MVC6 new Features, once again:

  1. MVC+Web API+Web Pages = MVC6
  2. No System.Web
  3. Web pages & HTTP services is Unified
  4. Dependency injection built in
  5. Dynamic code compilation (Roslyn compiler)
  6. Open source &
  7. Support cross-platform build & run.
  8. Can be hosted in IIS or self-hosted(Outside IIS)

Ok, now let’s add a WebApi Controller to perform CRUD operation to database table.


In Solution Explorer add a new api folder, right click on it > Add New Item > Web API Controller Class > Add. Modify the initial template.

API Controller


You may notice that there is a new pattern [ ] in MVC6 attribute route, which is [RouteToken]. This mean that the route token is automatically take the controller name.

Another thing is, we know Web API produces XML by default, now in MVC 6 we can set an attribute to change default produces to JSON type by putting attribute in Class label or on method label. In our case we have set it on method label.





So our Web API is ready to dealing with data to database, it’s time to work with client side scripting.



Our WebAPI is ready to deal with data from server. Now we are going to work in client-side code with typescript (.ts) files.

First of all we need to create a master page to present our views in it.


Then we need to point this html file while app start, so let’s go to the startup.cs file to add below code snippet.

This is the configuration for the default files Middleware.


need to add library

Now add script library reference to the html page & define view point to load our app component views.

Then we need to reference our bootstrap file in our page, that import & enable the our angular script to the page.

Let’s put together all those in Index.html file.



Bootstrap, Model, Component & Route









Let’s take a closer look at below code snippet, we have the service method call in hare, but the unknown term Subscribe – What is it for? Below we have a simple explanation.

Subscribe: The subscriber function to be passed to the Observable constructor.



In our service file we have Http service [Get, GetByID, Post, Put, Delete] that connect with WebAPI to perform operation Create, Read, Update & Delete.

GET ALL: Performs a request with get http method. For Collection of Object

GET By ID: Performs a request with get http method. For Single Object

POST: Performs a request with post http method.

PUT: Performs a request with put http method.

DELETE: Performs a request with delete http method.

Observable : [Observable<T>] A representation of any set of values over any amount of time. This the most basic building block of RxJS.

Let’s put it together in Contact.service file.


Let’s discus about form in angular2, there are two strategy of angular2 form

  1. Template-driven
  2. Model-driven


In template-driven form directive are added declaratively in the template.

Noticed that the validator is added declaratively with the input element “required”.


In our sample app we have used model-driven form that has ngFormModel & ngFormControl. Here ngFormControl is bind with input element to get the input values through the control.

ngFormModel: binding it to a controller variable “contactForm”


ControlGroup is contain of several Controls.

An Injected FormBulder use the builder to create the control group which is pass as key value pairs.

The validations are also checked in our component. Below we have our Model-driven complete form.



Here is the complete contact view page which we have used in our application.



This is all about from our angular section in our sample app, that we have used to perform client-end operation, now it’s time to build & run the application. Next we will get overview on server configuration.


Configure Server:

Outside IIS (Weblistener): We can host & run our application without an IIS environment, we need to add command object that tell the hosting to use the HTTP server weblistener (Windows-only).

Make sure the dependencies is exist

Now go to solution explorer right click project > properties > Debug , change the profile to OutsideIIS.


Set launch URL, then save & run the application.


Server will start with hosting environment details


The application is running on url http://localhost:5000 with details request info.


Inside IIS (Kestrel): Kestrel is a cross-platform web server which is run behind IIS or Nginx.


Change to IIS Express from navigation bar dropdown list to run the application using IIS. Ok let’s run our application to see the way how it’s work.


Here we can see the application output with welcome message & the data is listed from the server through our MVC6 –WebAPI. Here we can perform CRUD operation with required validation.


Source Code: I’ve uploaded the full source code to download/clone @github, Hope this will help 🙂

2 Replies to “CRUD using .Net Core, Angular2, WebAPI”

Leave a Reply to charan Cancel reply

Your email address will not be published. Required fields are marked *