Click here to Skip to main content
12,692,778 members (35,801 online)
Click here to Skip to main content
Add your own
alternative version


7 bookmarked

Using Stored Procedures in Entity Framework Model First Approach (Part I - Adding New Stored Procedure)

, 15 Apr 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
How to use Stored Procedures in Entity Framework Model First Approach

Before starting with this article, I expect that you have a basic idea on the Entity Framework - Model First approach and its use.

Here, I am explaining the step by step approach on how to use a newly created stored procedure in our C# code through the EntiFramework. Let's say we have created a new procedure named as 'GetAllPersons', now we can follow the below steps:


(Open EDMX file right click and choose the option 'Update Model from Database...')


Now in this below screen choose save entity connection string in Web.Config as the name given at the time of creation. Then click on NEXT.


Select 'Add' tab and there select the particular stored procedure you want to add to entity model.

Unselect the option 'Pluralize or singularize object names' and click on FINISH button.


Rename the Entity added automatically.

Open the Model Browser now and we can see the 3 things added automatically in entity model like:

  • StoredProcedure - The procedure we added recently.
  • Function Import - Function which actually will be called in code by the object of the entity context.
  • ComplexType - The entity returned by the SP or the Function generated.

The Function and ComplexType can be renamed as we want them.

Rename the ComplexType name as our feasibility:


Edit the Function Import as per requirement.

Open the function import in EDIT mode.

Rename the function and select appropriate Stored Procedure and ComplexType.

After editing done:


Finish & Run Custom Tool.

Now after all the changes are being made to make the latest change available in code, we have to run the custom tool as mentioned in the below image. This step is to update the classes from the model to avail latest changes made to it in code behind.

For more information on this custom tool and tt files, please refer to the description here.


Get result in code file.

To get the list of all the Persons as returned by the function imported:

Create the object of the Entity context class:

Test_SPEntities objTestSPEntity = new Test_SPEntities();

Call the function and get the result as list of the particular Complex Type:

List<GetAllPersonsEntity> listAllPersons = objTestSPEntity.GetAllPersons().ToList();

Hope this will definitely be of help for the guys who just started with the basics of the Entity Framework.


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


About the Author

SRS(The Coder)
Software Developer
India India
Having more than 6 years of development experience in various Microsoft Technologies like :-
ASP.Net, MVC, SQL Server, WCF, MS Commerce Server, Xamarin etc.

Also hands on experience on the client side coding with JavaScript/jQuery and framework like AngularJS.Worked with third party controls like Telerik and DevExpress as well.

Very much interested in Microsoft technologies to explore and implement. Now started to helping coders who really need me.

My Blog

Microsoft Technology Master

Programming Communities

JS Fiddle | Stack Overflow


Microsoft MCTS Certification in ASP.Net 4.0

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Apr 2014
Article Copyright 2014 by SRS(The Coder)
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid