Click here to Skip to main content
15,909,518 members
Articles / Programming Languages / SQL

Insert, Update and Delete Function Mapping to Stored Procedures In Entity Framework 6

Rate me:
Please Sign up or sign in to vote.
4.44/5 (23 votes)
16 Oct 2014CPOL2 min read 51K   26   7
Insert, Update and Delete Function Mapping to Stored Procedures In Entity Framework 6

Introduction

It is a known fact that stored procedures are always better when compared with standalone queries whenever performance is considered. So, here we will use the ADO.NET Entity Data Model Designer to map the insert, update, and delete operations of an entity type to stored procedures. As we know very well, the default behaviour of each entity type EDM on insert, update and delete operations is executing auto generated queries.

Observation

Let us assume that we have a single table Department in our database MyOrg and we will add it to our EDM MyOrg.edmx in a console application named ModificationFunctions.

Image 1

The data in the table is:

Image 2

Now let us perform Insert, Update and Delete operations and observe the default behaviour.

C#
namespace ModificationFunctions
{
    class Program
    {
        static void Main(string[] args)
        {
            using (MyOrgEntities OE = new MyOrgEntities())
            {
                //Insert Operation
                OE.Departments.Add(new Department() { DName = "Production", HOD = "Zing", Gender = "M" });
                OE.SaveChanges();

                //Update Opertation
                var dept = OE.Departments.Where(x => x.Did == 1003).FirstOrDefault();
                dept.HOD = "Martin";
                OE.SaveChanges();

                //Delete Operation
                var dept1 = OE.Departments.Where(x => x.Did == 1013).FirstOrDefault();
                OE.Departments.Remove(dept1);
                OE.SaveChanges();
            }
        }
    }
}

To observe the default behavior, I will use IntelliTrace Window.

Auto generated insert query:

Image 3

Auto generated update query:

Image 4

Auto generated delete query:

Image 5

Implementation

Step 1

Now let us try to change this default behaviour by replacing all these queries with user defined stored procedures, i.e.,

SQL
Create Proc InsertDepartment
(@DName as Varchar(50), @HOD as varchar(50), @Gender as Varchar(50))
as
INSERT INTO Department (DName,HOD,Gender) VALUES (@DName,@HOD,@Gender)

Create Proc UpdateDepartment
(@DName as Varchar(50), @HOD as varchar(50), @Gender as Varchar(50),@Did as int)
as
Update Department Set DName=@DName,HOD=@HOD,Gender=@Gender where Did=@Did

Create Proc DeleteDepartment
(@Did as int)
as
Delete from Department where Did=@Did

Step 2

Now go to MyOrg.edmx designer surface, right-click and select update model from database and then select all these 3 stored procedures from Stored Procedures and Functions and make sure that you uncheck the option Import selected stored procedures and functions into the entity model and then click Finish button. As shown below:

Image 6

Step 3

Now right-click the entity type Department to map the insert, update and delete operations and select Stored Procedures Mapping. The Map Entity to Functions view of the Mapping Details window appears as shown below:

Image 7

Step 4

From the drop-down list, select the stored procedure to which the insert, update and delete operation is to be mapped. The window is populated with default mappings between entity properties and stored procedure parameters as shown below and save .edmx file.

Image 8

Now let us examine the same insert, update and delete operations using IntelliTrace Window and now it should use the imported stored procedures for insert, update and delete operations instead of default queries.

Insert

Image 9

Update

Image 10

Delete

Image 11

Thanks for reading!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder ManzoorTheTrainer.com
India India
Manzoor is a Microsoft Certified Trainer who has been working on MS .Net technologies for more than a decade. Apart from development he is also passionate about delivering training on various MS .Net technologies and he has 10+ years of experience as a software development teacher. He writes articles for code-project as well. His YouTube channel has 1 million hits. He is the founder of ManzoorTheTrainer portal.

"I focus on simplifying, complex concepts..." - ManzoorTheTrainer

Founder of www.ManzoorTheTrainer.com [Free .net video tutorials on MS SQL Server, Asp.Net, C#.Net, Ado.Net, Entity Framework, MVC, Web Services, Android]

Comments and Discussions

 
AnswerNice tutorial Pin
Kashif Ahmad Khan8-Jul-17 21:48
Kashif Ahmad Khan8-Jul-17 21:48 
QuestionNice article Pin
Member 344073731-Mar-17 10:52
Member 344073731-Mar-17 10:52 
Questionremove InsertFunctionMapping Pin
marioleal26-May-15 3:16
marioleal26-May-15 3:16 
Questionnice one Pin
Member 1160591530-Apr-15 0:37
Member 1160591530-Apr-15 0:37 
QuestionProgramatically SP Mapping Pin
Member 382109320-Oct-14 13:35
Member 382109320-Oct-14 13:35 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun17-Oct-14 3:20
Humayun Kabir Mamun17-Oct-14 3:20 
GeneralRe: My vote of 5 Pin
Mohd Manzoor Ahmed20-Oct-14 16:59
professionalMohd Manzoor Ahmed20-Oct-14 16:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.