Skip to content

CRUD Operations in Web API Using Stored Procedures

In this tutorial, I implement basic CRUD operations in a Web API using stored procedures created in a SQL Server database. Previously we built applications using the Entity Framework code-first approach without stored procedures; now we implement them here, step-by-step.

Steps to follow:

  • Create a Web API project.
  • Install all the required packages from the NuGet Package Manager.
  • Set up the connection string in the appsettings.json file.
  • Create the model class for the CRUD operations; here we use an Employee model class.
  • Create a DbContext class and add a DbSet property for the model class.
  • Register the DbContext class in the Program.cs file.
  • Create a Repository folder and create an IEmployeeRepo interface in it.
  • Create a Service folder and create an EmployeeService class in it.
  • Register the repository and service classes in the Program.cs file.
  • Create a controller for employee CRUD and define the methods in it.
  • Create stored procedures in SQL Server for insert, update, delete and select.

To start, create a Web API project using the Web API template, as shown in the image below. Name the project, then select the default options and the target framework, as shown in the images below.

Selecting the ASP.NET Core Web API project template in Visual Studio
Configuring the Web API project name and location in Visual Studio
Web API Additional information dialog with .NET 6.0, controllers and OpenAPI support enabled

Install all the required packages from the NuGet Package Manager, as shown in the image below.

Installing the required NuGet packages

After installing the packages, your Solution Explorer will look like the image below.

Solution Explorer showing the installed NuGet packages

Now add an Employee model class with the properties below.

 [Key]
 public int EmployeeId { get; set; }
 public string EmployeeName { get; set; }
 public string EmployeeDescription { get; set; }
 public int EmployeeSalary { get; set; }
public int YearOfService { get; set; }

Now add a DbContext class with the code below.

 public class ApplicationDbContext:DbContext
 {     
      public ApplicationDbContext(DbContextOptions
      <ApplicationDbContext> options) 
      : base(options)
      {

      }
      public DbSet<UserRegistration>tblUsers{get;set;}
      public DbSet<Employee> Employees {get;set;}
}

Now add the connection string to the appsettings.json file.

"ConnectionStrings": {
  "DBCS": "server=DESKTOP-1CO7VJ1\\SQLEXPRESS;
  database=FirstCrudDB;
    Trusted_Connection=true;encrypt=false;"
}

Now add a Repository folder to the project, create an interface named IEmployeeRepo and write the methods below in it.

public interface IEmployeeRepo
    {
        public Task<List<Employee>> GetEmployeeListAsync();
        public Task<IEnumerable<Employee>> GetEmployeeByIdAsync(int Id);
        public Task<int> AddEmployeeAsync(Employee Employee);
        public Task<int> UpdateEmployeeAsync(Employee Employee);
        public Task<int> DeleteEmployeeAsync(int Id);
    }
Employee repository interface with async CRUD methods

Now add a Service folder to the project, create an EmployeeService class in it and write the code below.

EmployeeService class calling stored procedures with Entity Framework Core
GetEmployeeListAsync and UpdateEmployeeAsync using FromSqlRaw and ExecuteSqlRawAsync

Now add the highlighted code to the Program.cs file to configure the database connection and register the employee services.

Registering the DbContext and employee service in Program.cs

Now create the stored procedures in the SQL database, as shown in the images below.

UpdateEmployee stored procedure in SQL Server
DeleteEmployeeByID and GetEmployeeByID stored procedures
AddNewEmployee stored procedure inserting into the Employees table
GetEmployeeList stored procedure selecting all employees

Now create an API controller named Employee and write the code, as shown in the images below.

EmployeeController with the GetEmployeeList and GetEmployeeById endpoints
AddEmployee (POST) and UpdateEmployee (PUT) actions in the Employee API controller
DeleteEmployee (DELETE) action in the Employee API controller

Now run the application. You get the Swagger UI, as shown in the images below.

Swagger UI listing the Employee API endpoints
Swagger response from GET api/Employee/getemployeelist