React js with Asp.net Web API – CRUD Operations part1

n this article, we’ll create a CRUD application using React js with Web API. CRUD Operations Insert Update and Delete will be implemented inside an Asp.net Web API Using Entity Framework and then consumed from an Angular 5 Application.

Following tools and modules are used for this project :
– react CLI
– react js
– ngx-Toastr (npm package)
– Bootstrap and Font-Awesome Icons
– VS Code & Visual Studio Editor
we assume that you have installed required packages and software for angular 5 application development.
Download project source code from GitHub : react js With Web API – CRUD Operations.

Create SQL Server DB

First of all, let’s create a database to work with. I use Management Studio to create and manage SQL database.
Created a Database with name ‘WebAPIDB’, In this application we’ll deals with details of employees. so an employee table is created using following SQL Script.
Here EmployeeID column is the Primary Key and IDENTITY column for the table. Because of IDENTITY specification, we don’t insert values into EmployeeID column. SQL Server will take care of that. It will start from 1 and incremented by 1 upon new record insertion.

Create Web API Application

Database is ready, Now let’s create an Asp.Net Web API project.
Open Visual Studio, Go to File > New > Project (Ctrl + Shift +N).

then select Web API template.
So here we have created a brand new Web API Project. Now let’s add Entity Model for the DB ‘WEPAPIDB’ inside Models Folder.
Right Click on Models Folder > Add > New Item.
Name your Entity Model as DBModels.edmx.

Select Generate From database.

In Data Connection Window, Click On New Connection.

Provide SQL Server Instance Details and Select the DB.

As per previous window, we’ll save DB Connection string in WebConfig as DBModel. After creating this Entity Model there will be a class with this name (DBModel), we’ll create an object of this class in-order to interact with database.
After previous window, you may see an extra window as follows, if you have multiple Entity Framework Versions, then select one of them.

then select tables that we want to add inside the Entity Model.

Click on Finish. Diagrammatic Representation of EF Model looks like this.
Inside this DBModels.edmx, you can see a DBModels.Context.cs file for DBModel Class. we’ll create an object of this class to work with database.
DBModels.tt > Employee.cs contains Employee Class, which contains properties corresponding to SQL Table Columns, So this is our Model Class.
Now let’s add Employee Controller, before that don’t forget to Re-build your solution. To create a Controller, right click on controllers folder then click on Add > Controller…, Then select Web API 2 Controller with actions, Using Entity Framework Scaffolding Mechanism.then following window will be shown.

It will create Web API Controller Employee using Employee Class from Entity Model. Created controller contains web methods GET,POST,PUT and DELETE for CRUD operations READ INSERT UPDATE AND DELETE respectively. These default web methods contains model validations, we don’t do model validation in this Web API project, Form validation can be done inside angular 5 application, Employee controller without validation looks like this
All these Web API methods are written using DB First Approach in Entity Framework. 
  make changes as follows

Now let’s check working of this Web API project, first of all let me add a test employee record inside Employee table.
Now let’s run our Web API project. then navigate /api/Employee URL. It will call GetEmployees Method to retrieve employee collection from SQL server table Employee.
View Employee Collection from DB through Web API
Here base URL is localhost:28750, we need this base URL to consume this Web API from Angular 5 Application. Thereby our Web API Project is working fine, finally let me truncate the test record from employee table.