How To Pass Generic List Using Dapper In MVC

Many times we need to insert list of records into the database instead of one by one , So in this article we will learn how to pass the generic list using Dapper.NET ORM in MVC .Please read my previous article  to understand the basics about MVC:
Before going to read this article I hope you are aware about Dapper.NET ORM , Let us look into the following example .
Suppose you have following Model class
Download Aspose : API To Create and Convert Files

public class EmpModel
        [Display(Name = "Id")]
        public int Empid { get; set; }
        [Required(ErrorMessage = "First name is required.")]
        public string Name { get; set; }
        [Required(ErrorMessage = "City is required.")]
        public string City { get; set; }

        [Required(ErrorMessage = "Address is required.")]
        public string Address { get; set; }


Now consider following stored procedure we have
Create procedure [dbo].[AddNewEmpDetails]  
(  @Id int=null),
   @Name varchar (50),  
   @City varchar (50),  
   @Address varchar (50)  
   Insert into Employee values(@Name,@City,@Address)  

Now following is the function which is used to pass above stored procedure to Dapper.NET

public void AddEmployee(List<EmpModel> EmpList)
            //Passing generic List to stored procedure using Dapper.NET
                con.Execute("AddNewEmpDetails", EmpList, commandType: CommandType.StoredProcedure);

 In the above function
  •  Connection() is the method which contains the connection string .
  •  Con is the SqlConnection class object.
  • AddNewEmpDetails is the stored procedure.
  • EmpList is the reference of model class generic list
 Download Aspose : API To Create and Convert Files
I hope this article is useful for all readers. If you have any suggestion then please contact me.
Download Aspose : API To Create and Convert Files.

Post a Comment

Protected by Copyscape Online Copyright Protection
Copyright © Compile Mode