Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure into Entity Framework model

Can you add a stored procedure into an Entity Framework model (Model.edmx)?

I just did it in Visual Studio 2010, like this (except my stored procedure is now off the Add tab and is in the Refresh tab)

VS screenshot

My stored procedure is a simple SELECT statement, taking 1 parameter.

VS2010 seems to have added it into the Model, but I wonder where it is and how I can use it?

I can't see it anywhere in the diagram nor in Model.designer.cs

like image 795
Aximili Avatar asked Feb 01 '26 08:02

Aximili


2 Answers

You part the way there. It's a bit of a hidden process and multi-stepped. You have to include the Stored Procedures in your model by selecting them in the menu you have shown above.

Once you have done that you want to view the 'Model Browser'

pic of model browser

Then in the Model Browser, find the Model Store (This will have folders like 'Tables / Views'). Right click on your Stored Procedure and select 'Add Function Import'.

pic of context menu for function import

Once you have done this, you can map the Entities you will get back from your SP.

pic of function import dialog

You will then be able to access your entities via your Context in code, in a strongly typed way. Hope that helps.

like image 176
Darren Reid Avatar answered Feb 04 '26 00:02

Darren Reid


From the model diagram, right click and select Model Browser (or View/Other Windows/Entity Data Model Browser from the menu). In the Model.Store section, you should see Tables/Views, Stored Procedures/Functions and Constraints. Your stored procedures will be in the Stored Procedures/Functions section.

Check this link for a good explanation of how to use stored procedures with EF: http://msdn.microsoft.com/en-us/data/gg699321.aspx

like image 23
mcalex Avatar answered Feb 03 '26 23:02

mcalex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!