.NET Core

CRUD Operations In .NET Core 3.0 With Visual Studio 2019

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

  • .NET Core 3.0 (Download from here)
  • Visual Studio 2019. (Download from here)

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 –
  1. Helper
  2. 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);  
    }  
}
Faisal Pathan

Faisal Pathan is a founder of TheCodeHubs, .NET Project Manager/Team Leader, and C# Corner MVP. He has extensive experience with designing and developing enterprise-scale applications. He has good skills in ASP.NET C#, ASP.NET Core, ASP.NET MVC, AngularJS, Angular, React, NodeJS, Amazon S3, Web API, EPPlus, Amazon MWS, eBay Integration, SQL, Entity Framework, JavaScript, eCommerce Integration like Walmart, Tanga, Newegg, Group-on Store, etc. and Windows services.

View Comments

  • 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!

  • Thanx Faisal for this article . .

    I m really impressed by how you use SQL Server Stored Procedures in CRUD Operations.

Recent Posts

Testing hk

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Operation

Testing

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

Create and Used PIPE in angular

In this article, we have to show Create and Used PIPE in angular

2 years ago

TETS NEW

test

2 years ago