Introduction
Web API development is an essential component in the present development scenario as it allows the creation of applications that scale and respond to multiple services and platforms’ needs. One of the bedrock processes in API creation is CRUD operations that allow for the foundational interfacing with the application’s data. If you are ‘green’ concerning .NET Core, beginning with CRUD operations makes perfect sense because it gives you the fundamentals of building a Web API and working with the data.
In this guide, we will guide you through the process of creating a .NET Core 8 Web API project and the definition of the endpoints for all the CRUD operations. There is effective practice in working with data defined by the Entity Framework Core, which is an ORM system designed to interact with databases in .NET. At the end of this article, you will have created a working Web API capable of data manipulation and will have grasped the basic concepts of RESTful design in .NET Core.
This article targets an aspiring backend developer in .NET Core 8 API development and an amateur front-end developer who is hoping to refine their skills. As such, if you fall within any of the two categories, you are guaranteed to learn how to start building APIs in .NET Core 8. Let’s walk through this beginning stage.
Prerequisites
- Visual Studio 2022
- .NET Core 8
- MS SQL Server
- Basic Understanding of C#
Open Visual Studio 2022 and Choose "Create a new project".
On the Create a new project page, search for "Web API" on the search bar, select the project template and press the "next" button.
On the configuration of the project, enter the Project Name and choose the check box to keep the solution file and project in the same directory.
On the additional information page of the project, we need to configure the version of the Framework and then check the boxes as per the screenshot and then press "create". It will create the project with the basic files to get started.
Initially, the project folder structure looked like this.
First, we need to install the packages required for the ORM to interact with the Database. To install the packages, Right click on the solution and choose "Manage NuGet Packages... "
On the NuGet page, search for the below two packages and install the versions above 8.
- Microsoft.EntityFrameworkCore.Tools
- Microsoft.EntityFrameworkCore.SqlServer
Then, Right-click on the solution. Create a new Class file named Employee.cs and paste the code.
namespace EmployeePortal.Models.Entities
{
public class Employee
{
public Guid Id { get; set; }
public required string Name { get; set; }
public required string Email { get; set; }
public required string PhoneNumber { get; set; }
public decimal Salary { get; set; }
}
}
Then, we need to create a DB Context file for the application that holds the configuration for the ORM and its Entities. Create a file named ApplicationDbContext.cs and paste the below code.
using EmployeePortal.Models.Entities;
using Microsoft.EntityFrameworkCore;
namespace EmployeePortal
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Employee> Employees { get; set; }
}
}
Then, we need to add a DB connection string to the appsettings.json file.
"ConnectionStrings": {
"DefaultConnection": "Server=your_server_name;Database=your_database_name;User Id=your_username;Password=your_password;TrustServerCertificate=True;"
}
Then, we need to add the SQL Server services to our Program.cs file and add the below code to the program file under the services, which tells the application to use the SQL server from the connection string.
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(
builder.Configuration.GetConnectionString("DefaultConnection")));
Now, we add a migration to create a snapshot of our entities from the application because we are using the Code First Approach in EF. So, we need to specify the entities and their relationships, and then we run the migration which will create a Database and tables based on the relationship of the entities.
Open the package manager console and run the following commands.
- add-migration "initial one": generates a migration file based on the current state of your data models compared to the database schema.
- update-database: applies the migration to your database, creating or altering tables, columns, or relationships as defined in the migration file.
Now, If you open the SQL server, you can see the database and the tables.
We need to create a Web API controller to fetch the data from the database through the endpoints. Right-click on the controller's folder and choose to add a new Web API Controller.
Then, add the name as EmployeesController.cs and paste the code below.
using EmployeeAdminPortal.Data;
using EmployeeAdminPortal.DTO;
using EmployeeAdminPortal.Models.Entities;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace EmployeeAdminPortal.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeesController : ControllerBase
{
private readonly ApplicationDbContext _dbContext;
public EmployeesController(ApplicationDbContext dbContext)
{
_dbContext = dbContext;
}
[HttpGet]
public IActionResult GetEmployees()
{
return Ok(_dbContext.Employees);
}
[HttpGet]
[Route("{id:guid}")]
public IActionResult GetEmployeeById(Guid id)
{
var employee = _dbContext.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
return Ok(employee);
}
[HttpPost]
public IActionResult AddEmployee(EmployeeDto employeeDto)
{
var employee = new Employee
{
Name = employeeDto.Name,
Email = employeeDto.Email,
PhoneNumber = employeeDto.PhoneNumber,
Salary = employeeDto.Salary
};
_dbContext.Employees.Add(employee);
_dbContext.SaveChanges();
return StatusCode(StatusCodes.Status201Created);
}
[HttpPut]
[Route("{id:guid}")]
public IActionResult UpdateEmployee(Guid id, UpdateEmployeeDto employeeDto)
{
var employee = _dbContext.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
employee.Name = employeeDto.Name;
employee.Email = employeeDto.Email;
employee.PhoneNumber = employeeDto.PhoneNumber;
employee.Salary = employeeDto.Salary;
_dbContext.SaveChanges();
return Ok(employee);
}
[HttpDelete]
[Route("{id:guid}")]
public IActionResult DeleteEmployee(Guid id)
{
var employee = _dbContext.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
_dbContext.Employees.Remove(employee);
_dbContext.SaveChanges();
return NoContent();
}
}
}
Also, you need to create a new folder for DTO(Data Transfer Objects), which is used to transfer data between layers or services within an application.
Create two DTO files named EmployeeDto.cs and UpdateEmployeeDto.cs.
// Employee DTO
namespace EmployeePortal.DTO
{
public class EmployeeDto
{
public required string Name { get; set; }
public required string Email { get; set; }
public required string PhoneNumber { get; set; }
public decimal Salary { get; set; }
}
}
UpdateEmployeeDto.cs
// Update Employee DTO
namespace EmployeePortal.DTO
{
public class UpdateEmployeeDto
{
public string? Name { get; set; }
public string? Email { get; set; }
public string? PhoneNumber { get; set; }
public decimal Salary { get; set; }
}
}
Then, build and run your application. You can able to see the endpoints of our application on the chrome with the help of Swagger.
You can request the endpoints using the necessary information.
Endpoints and their use cases with return types
- Get All Employees: it is GET api/employees. Method: GetEmployees(). Purpose: Fetches all the employees from the store. Response: Returns an Ok (HTTP 200) status with the body, which contains the list of employees.
- Get Employee by ID: it is GET api/employees/{id}. Method: GetEmployeeById(Guid id). Purpose: Use unique IDs to retrieve an employee. Response: Returns an Ok (HTTP 200) status and data about an employee when such is available, however, there is a risk that if no such employees can be traced then one will be returned as NotFound (HTTP 404).
- Add a New Employee: it is POST api/employees. Method: AddEmployee(EmployeeDto employees). Purpose: Whenever an employee Dto record is transmitted to the application, the application updates the defined employees in the store. Response: Returned 201 Created status whenever an employer is successfully added.
- Update Employee: it is PUT api/employees/{id}. Method: UpdateEmployee(Guid id, UpdateEmployeeDto employeeDto). Purpose: One has to search the employee by the unique ID and change his information. Response: If the updates work effectively, they return an Ok (HTTP 200) status, while the NotFound (HTTP 404) will be resolved in situations where no suitable culprit persons are available.
- Delete Employee: it is DELETE api/employees/{id}. Method: DeleteEmployee(Guid id). Purpose: When databases are directed to delete certain profiles, the corresponding unique ID numbers are used. Response: If it works, returns NoContent (HTTP 204) in situations where the deletion was successful, whereas NotFound (HTTP 404) if no matching employee is found.
Conclusion
In this article, we explored a basic implementation of CRUD operations for managing employee data in a .NET Core Web API. By following these steps, we created endpoints to add, retrieve, update, and delete employees, using DTOs to encapsulate and simplify data transfer. This approach helps establish a solid foundation for building RESTful APIs and managing data flow in a secure and organized manner.