ASP.NET 4.0 and the Entity Framework 4 - Part 3 - Execute Stored Procedures Using the Entity Framework 4
page 3 of 9
by Vince Varallo
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 37814/ 64

Step 2: Add the Stored Procedures to the Entity Model

Open the OrderDB.edmx file in you project.  This assumes you created a web application as described in Part 1 of this series.  When you open the OrderDB.edmx file the entities will appear in the designer and the Model Browser window will show you the database with the Entities, Complex Types, and Associations listed in a folder structure.  Right click on the OrderDB.edmx node in the Model Browser and select Update Model From Database… from the pop-up menu.  The Update Wizard should appear and it should recognize the five stored procedures added in step 1.

Check the box next to the Stored Procedure node to check all the stored procedures and then click the Finish button.  The stored procedures will now appear under the Stored Procedures in the Model Browser.

Right click on the UserAccounts_SelectAll stored procedure in the Model Browser and select Add Function Import… from the pop-up menu.  The Add Function Import dialog should appear.  This allows you to add a method to the OrdersDBContainer class that will execute this stored procedure. 

You need to choose the return type when the stored procedure is executed.  It would be nice if you could return a list of UserAccount entities but complex type properties aren't supported by the framework.  To get around this issue you need to create a new complex type that represents the return set from the stored procedure.  Since the SelectAll and SelectById stored procedures both return the same fields we'll create a single complex type that both return. 

Click the Get Column Information button.  This will determine the fields that are returned by the stored procedure.  Once the fields are listed, click on the Create New Complex Type button.  When you click the button the Complex option will automatically be selected and the name of the complex type that will be created is added to the drop down list next to the Complex type option.  By default the name of the complex type is the name of the stored procedure with the word Result appended to it.  Since we'll share this complex type with two stored procedures we'll make the name generic.  Change the name to UserAccounts_Select_Result.  Now click the OK button.

You should see the UserAccounts_SelectAll function under the Function Imports folder in the Model Browser.  You should also see the new UserAccounts_Select_Result complex type under the Complex Types node.

 

Now right click on the UserAccounts_SelectById procedure in the Model Browser.  Select Add Function Import again and select Complex type for the return type and select the UserAccounts_Select_Result complex type.  Click the OK button.

Now you need to associate the Insert, Update, and Delete stored procedures with the UserAccount Entity.  To do this you should right click on the UserAccount entity in the designer.  Select Stored Procedure Mapping from the pop-up menu.  You should see Mapping Details window.

Click on <Select Insert Function> from the first row and select UserAccounts_Insert from the drop down list.  The list of parameters will appear in the grid.  You need to map the property from the entity to the parameter in the stored procedure.  Since this procedure returns the Id of the newly added record you need to define which property will be set by the return value of the procedure.  Under "Result Column Bindings" enter Id where it displays <Add Result Binding>.  It is important that your stored procedure defined above contains the line SELECT SCOPE_IDENTITY() AS Id.  If you don't it won't work.  The Mapping Details should look like the following image.

Now you need to associate the Update stored procedure.  Set the <Select Update Function> to the UserAccounts_Update stored procedure.  You'll have to manually map the AuditFields_UpdateDate to the AuditFields.UpdateDate property.

The last step is to map the Delete stored procedure.  Set the <Select Delete Function> to the UserAccounts_Delete stored procedure.  The Id parameter should be mapped correctly because it is named the same as the property.

Save your project at this time.  The OrdersDBContainer now "knows" about the stored procedures and they are mapped to the UserAccount entity to allow for insert, update, and delete.


View Entire Article

User Comments

Title: Greate Writer   
Name: Alex
Date: 2012-09-04 11:54:02 AM
Comment:
Vince, is a great author. Looking forward from him a new book in asp.net 4.5 enterprise application development with all the latest features. EF, etc
Title: Stored Procedure doesn't return columns   
Name: Oscar
Date: 2011-12-28 10:18:54 AM
Comment:
I'm following your tutorial (very well done and thanks). When I reached the point to add the function import for the Select_all stored procedure I get the answer that the Stored Procedure doesn't return columns (My best translation from spanish). If I execute it from the sms client works fine.
Looked around alredy and didn't find anything. May be you could give a hint on where to look for a solution?
Title: Excellent   
Name: John
Date: 2011-05-05 4:00:03 PM
Comment:
excellent article
Title: Thanks a Ton!   
Name: Vipul
Date: 2011-03-22 12:02:05 AM
Comment:
Very helpful article indeed!
Title: Excellent   
Name: Sathiya
Date: 2011-01-09 6:04:12 AM
Comment:
Varallo,
Great Article and very impressive. Please post more article related to Framework 4.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-26 3:45:26 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search