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.
Install all the required packages from the NuGet Package Manager, as shown in the image below.
After installing the packages, your Solution Explorer will look like the image below.
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);
}
Now add a Service folder to the project, create an EmployeeService class in it and write the code below.
Now add the highlighted code to the Program.cs file to configure the database connection and register the employee services.
Now create the stored procedures in the SQL database, as shown in the images below.
Now create an API controller named Employee and write the code, as shown in the images below.
Now run the application. You get the Swagger UI, as shown in the images below.