Introduction
In this article, we'll show you how to use Entity Framework to construct a WEB API in ASP.Net Core 6. ASP.NET Web API makes it easier to create HTTP(s) services that connect to various clients such as browsers and mobile devices. Building RESTful apps is best accomplished on the ASP.NET Web API platform. Use the HTTP verbs GET, POST, PUT, and DELETE and test it using Swagger.
What is WebAPI and Why do we need it
API stands for Application Programming Interface. The HTTP Protocol is utilized by Web API, a concept that is used to increase an application's capabilities. A web-based application alone is no longer sufficient to reach all of its users or clients. People today are incredibly intelligent, which means they use technology like the iPhone, mobile phones, tablets, etc. in their daily lives. Various useful apps are available for these devices that can simplify users' lives. We can state that we are migrating from the web to the world of apps. Therefore, if we want to quickly, securely, and simply expose our service data to browsers and all of these current device apps, we need to use an API that works with both.
On the left, as you can see in the picture below, are the three applications, and on the right, as you can see in the picture below, is the database.

Our objective is to develop communication between the database and all three of these applications. A new Web API project will be added. The database will be used in this Web API Project's interactions. Also, as can be seen in the picture below, all three applications will only communicate with the database via the Web API Project.

As a result, the database is not directly accessible through these websites, Android, or iOS applications. They just need to communicate with the Web API project; the Web API project is in charge of dealing with database interactions. We require a Web API for our project because all of the business logic will be written in the Web API project alone. Web API serves as a bridge between the front-end and back-end results.
Describe HTTP Methods
Every time a client sends a request to a server, it includes an HTTP method that specifies what the client wants the server to do with the requested resource. These requested actions are represented by HTTP methods. Typical HTTP methods, for instance, can fetch data from a server, submit data to a server for processing, delete something from the server's database server, etc. The term CRUD, which stands for Create, Read, Update, and Delete, can be used to summarise a large portion of application functionality. Each of these actions has a corresponding four HTTP method which is given below
- POST-Create
- GET-Read
- PUT-Update
- DELETE-Delete
How to create Web API
Step 1
Create a new project in Visual Studio and firstly we will install some packages from the manage Nuget package.
- MicroMicrosoft.Entity Frameworkcore.InMemory
- MicroMicrosoft.Entity Frameworkcore.sql server
- MicroMicrosoft.Entity Frameworkcore.Tools
Step 2
In the solution create a new folder and name its model inside a model to create a class and name as Contacts inside the Contacts we have a few properties the first property is a Guit id and after that, we have a string of name which will have the contacts name after that we have an email address for the contact and that to be of type string as well and then we have a phone number which is for the contact will be of type long and lastly we have a property of address which will store the physical address of the contact and that will be of type string
public class Contact {
public Guid id {
get;
set;
}
public string name {
get;
set;
}
public string email {
get;
set;
}
public long phone {
get;
set;
}
public string address {
get;
set;
}
}
Step 3
it's now time to create a class file that will connect to our database and will act like a domain access layer because we are using an entity framework for we use the concept of the context which acts as a class that talks to the database no matter what type it is and it also features and retrieves data for us so.
Let's create a DB contact click on a solution in a new folder call its data and insert data create a new class file name this is the contacts API which is the API name followed by the word DB contact so ContactsAPIDbContext inside the class this class will inherit from the class Db context.
Click the control that imports the dependencies using the microsoft.net Framework installed when we install the in-memory database package after that generate a constructor with which we are passing the options and the options are being passed to the base class after that in any context
We have to create properties that will act as tables for the entity framework core so creating a property only needs one property which is our contacts model. We only want to retrieve update delete and add contacts so we have a Dbset property of type contact.






Join the conversation! Your thoughts help the community grow.