Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Entity Framework Objects from Stored Procedures

When using Entity Framework 4, how do you create a single entity from a stored procedure?

like image 631
Achilles Avatar asked Dec 01 '10 20:12

Achilles


People also ask

Can we use stored procedure in Entity Framework?

You can use stored procedures either to get the data or to add/update/delete the records for one or multiple database tables. EF API creates a function instead of an entity in EDM for each stored procedure and User-Defined Function (UDF) in the target database.


1 Answers

After you add the stored procedure to the model, from the Model Browser right-click the stored procedure under the Store node and select 'Add Function Import'. In that dialog, indicate what entity should be created:

alt text

You can then use that stored procedure like this:

Artist a = ctx.SelectArtist(id).SingleOrDefault();

Edit

Based on the comments, it sounds like you want to use stored procedures to perform inserts, updates, and deletes. If you right-click on the entity in the model browser and select 'Stored Procedure Mapping', you'll get a window that lets you specify which stored procedures to use. MSDN has a walkthrough on how to do this.

HTH

like image 55
Jeff Ogata Avatar answered Oct 02 '22 01:10

Jeff Ogata