Entity Framework - Stored Procedures


The Entity Framework has the ability to import a Stored Procedure as a function. We can also map the result of the function back to any entity type or complex type. Stored procedures and user-defined functions (UDFs) in the database are represented as functions in entity framework. EDM won't have any entity for the stored procedures in the EDM designer.

The following is the procedure to import and use a Stored Procedure in Entity Framework.

1. New project from File → New → Project.

Entity Framework - Stored Procedures

Step 2 - In Server explorer right-click on your database.
Step 3 - Select New Query and write stored procedure create script


Step 4 - Right click on the editor and select Execute.

Entity Framework - Stored Procedures

Step 5 − Right-click on your database and click refresh. 

Entity Framework - Stored Procedures
Step 6 − Right-click on your database and click refresh. You will see that a stored procedure is created in your database.
Step 7 − Right-click on the project name in Solution Explorer and select Add → New Item.
Step 8 − Then select ADO.NET Entity Data Model in the Templates pane.

Entity Framework - Stored Procedures
Step 9 − Enter SPModel as name, and then click Add.
Step 10 − In the Choose Model Contents dialog box, select EF designer from database, and then click Next.

Entity Framework - Stored Procedures
Step 11 − Select your database and click Next.

Entity Framework - Stored Procedures
Step 12 − In the Choose Your Database Objects dialog box click on tables, views.

Entity Framework - Stored Procedures
Step 13 − Select the GetUserMessageList function located under the Stored Procedures and Functions node and click Finish.
Step 14 − Select View → Other Windows → Entity Data Model Browser and right-click GetUserMessageList under Function Imports and select Edit.

Entity Framework - Stored Procedures
Step 15 - Edit function import dialog will be opened

Entity Framework - Stored Procedures
Step 16 − Click on Entities radio button and select DU_FND_Message from the combobox as return type of this stored procedure and click Ok.

Praesent mattis

Pellentesque viverra vulputate enim. Aliquam erat volutpat. Pellentesque tristique ante ut risus. Quisque dictum. Integer nisl risus, sagittis convallis, rutrum id, elementum congue, nibh. Suspendisse dictum porta lectus. Donec placerat odio vel elit.

Read More