In this article, we’ll learn how to perform CRUD operations with .NET Core 3.0 and Visual Studio 2019. We will use a dapper to perform CRUD operations.
Recommended Prerequisites
Create a Database, Tables, and Stored Procedures.
First, we need to create a SQL Server database, tables, and stored procedure which we need to use in the application.
Here, I’m creating a database, “CoreMaster” and a table “Jobs”.
CREATE TABLE [dbo].[Job]( [JobID] [int] IDENTITY(1,1) NOT NULL, [JobTitle] [nchar](250) NULL, [JobImage] [nvarchar](max) NULL, [CityId] [int] NULL, [IsActive] [bit] NULL, [CreatedBY] [nvarchar](50) NULL, [CreatedDateTime] [datetime] NULL, [UpdatedBY] [nvarchar](50) NULL, [UpdatedDateTime] [datetime] NULL, CONSTRAINT [PK_Job] PRIMARY KEY CLUSTERED ( [JobID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Now, I’m going to create stored procedures for adding jobs, fetching job lists, and updating jobs.
The “Add Job” stored procedure is named “[SP_Add_Job]” which returns the inserted record’s job Id.
CREATE PROCEDURE [dbo].[SP_Add_Job] @JobTitle NVARCHAR(250) , @JobImage NVARCHAR(Max) , @CityId int , @IsActive BIT , @CreatedBY NVARCHAR(50) , @CreatedDateTime DATETIME , @UpdatedBY NVARCHAR(50), @UpdatedDateTime DATETIME AS BEGIN DECLARE @JobId as BIGINT INSERT INTO [Job] (JobTitle , JobImage , CityId , IsActive , CreatedBY , CreatedDateTime , UpdatedBY , UpdatedDateTime ) VALUES ( @JobTitle , @JobImage , @CityId , @IsActive , @CreatedBY , @CreatedDateTime , @UpdatedBY , @UpdatedDateTime ); SET @JobId = SCOPE_IDENTITY(); SELECT @JobId AS JobId; END;
2. Microsoft.EntityFrameworkCore.SqlServer.Design
3. Microsoft.EntityFrameworkCore.Tools
4. Dapper
Create Dapper Class and Interface
Now, create two folders –
- Helper
- Interface
In the Interface folder, add a new interface namely “IDapperHelper” and copy the below code and paste in that class.
using Dapper; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using System.Linq; using System.Linq.Expressions; using System.Threading.Tasks; namespace CoreDemo_3_0.Interfaces { public interface IDapperHelper : IDisposable { DbConnection GetConnection(); T Get<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure); List<T> GetAll<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure); int Execute(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure); T Insert<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure); T Update<T>(string sp, DynamicParameters parms, CommandType commandType = CommandType.StoredProcedure); } }
It is perfect time to make some plans for the longer term and it is time to be happy. I have read this post and if I could I desire to recommend you some attention-grabbing issues or tips. Perhaps you could write next articles regarding this article. I wish to learn more things about it!
thanks for sharing this article. this post is very helpful.
Hello Faisal . Please We wish you do this same project this time with BLAZOR
Thanx Faisal for this article . .
I m really impressed by how you use SQL Server Stored Procedures in CRUD Operations.
Thanks for reading and valuable feedback!