Introduction
In this article, we are going to discuss the implementation of Web API using entity framework core and Stored Procedure in SQL Server.
Agenda
- Implementation of .NET Core 6 Web API
- Implementation of Stored Procedures
Prerequisites
- .NET Core SDK 6
- SQL Server
- Visual Studio 2022
Implementation of .NET Core 6 Web API
Step 1
Create a new .NET Core Web API application

Step 2
Configure the application

Step 3
Provide additional information

Step 4
Project Structure

Step 5
Create a Product class inside the Entities folder
using System.ComponentModel.DataAnnotations;
namespace EntityFrameworkSP_Demo.Entities
{
public class Product
{
public int ProductId { get; set; }
public string ProductName { get; set; }
public string ProductDescription { get; set; }
public int ProductPrice { get; set; }
public int ProductStock { get; set; }
}
}
Step 6
Next, add a new DbContextClass inside the Data folder
using EntityFrameworkSP_Demo.Entities;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkSP_Demo.Data
{
public class DbContextClass : DbContext
{
protected readonly IConfiguration Configuration;
public DbContextClass(IConfiguration configuration)
{
Configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
}
public DbSet<Product> Product { get; set; }
}
}
Step 7
Later on, create IProductService and ProductService inside the Repositories folder
IProductService
using EntityFrameworkSP_Demo.Entities;
namespace EntityFrameworkSP_Demo.Repositories
{
public interface IProductService
{
public Task<List<Product>> GetProductListAsync();
public Task<IEnumerable<Product>> GetProductByIdAsync(int Id);
public Task<int> AddProductAsync(Product product);
public Task<int> UpdateProductAsync(Product product);
public Task<int> DeleteProductAsync(int Id);
}
}
ProductService
using EntityFrameworkSP_Demo.Data;
using EntityFrameworkSP_Demo.Entities;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
namespace EntityFrameworkSP_Demo.Repositories
{
public class ProductService : IProductService
{
private readonly DbContextClass _dbContext;
public ProductService(DbContextClass dbContext)
{
_dbContext = dbContext;
}
public async Task<List<Product>> GetProductListAsync()
{
return await _dbContext.Product
.FromSqlRaw<Product>("GetPrductList")
.ToListAsync();
}
public async Task<IEnumerable<Product>> GetProductByIdAsync(int ProductId)
{
var param = new SqlParameter("@ProductId", ProductId);
var productDetails = await Task.Run(() => _dbContext.Product
.FromSqlRaw(@"exec GetPrductByID @ProductId", param).ToListAsync());
return productDetails;
}
public async Task<int> AddProductAsync(Product product)
{
var parameter = new List<SqlParameter>();
parameter.Add(new SqlParameter("@ProductName", product.ProductName));
parameter.Add(new SqlParameter("@ProductDescription", product.ProductDescription));
parameter.Add(new SqlParameter("@ProductPrice", product.ProductPrice));
parameter.Add(new SqlParameter("@ProductStock", product.ProductStock));
var result = await Task.Run(() => _dbContext.Database
.ExecuteSqlRawAsync(@"exec AddNewProduct @ProductName, @ProductDescription, @ProductPrice, @ProductStock", parameter.ToArray()));
return result;
}
public async Task<int> UpdateProductAsync(Product product)
{
var parameter = new List<SqlParameter>();
parameter.Add(new SqlParameter("@ProductId", product.ProductId));
parameter.Add(new SqlParameter("@ProductName", product.ProductName));
parameter.Add(new SqlParameter("@ProductDescription", product.ProductDescription));
parameter.Add(new SqlParameter("@ProductPrice", product.ProductPrice));
parameter.Add(new SqlParameter("@ProductStock", product.ProductStock));
var result = await Task.Run(() => _dbContext.Database
.ExecuteSqlRawAsync(@"exec UpdateProduct @ProductId, @ProductName, @ProductDescription, @ProductPrice, @ProductStock", parameter.ToArray()));
return result;
}
public async Task<int> DeleteProductAsync(int ProductId)
{
return await Task.Run(() => _dbContext.Database.ExecuteSqlInterpolatedAsync($"DeletePrductByID {ProductId}"));
}
}
}
- FromSqlRaw method is used to execute SQL commands against the database and returns the instance of DbSet
- ExecuteSqlRawAsync is used to execute the SQL commands and returns the number of rows affected
- ExecuteSqlInterpolatedAsync executes the SQL command and returns the number of affected rows
Step 8
Add database connection string inside the appsettings.json file







jose loraPosted Oct 24, 2023, 1:09 AM
EXCELENT WORK
sivanathan BalasubramanianPosted Feb 3, 2023, 12:25 PM
What do I do when we have a foreign key situation.Like Product and Order.Kindly guide me
Nguyen CuongPosted Dec 15, 2022, 2:30 PM
Hey Guy, you can make topic about Unit Of Work With Generic Repository Implementation Using Stored Procedure In .NET Core 6 Web API. Tks u