Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping select stored procedures in entity framework

My scenario I'm using Visual Studio 2010 with Entity Framework 4.1 I have a legacy database with many tables and many stored procedures. I'm writing an ASP.NET C# program using MVC 3

I have adopted the 'database first' design using ADO.NET DbContext so I have an edmx with all the models and associations and navigation properties nicely set up. I can map the insert, update, delete procedures to the relevant models. I've used 'Function Import' to import other stored procedures. However, I can't find a way to map my Select procedures to select actions (select by id, select list, select by filter etc).

EF seems to use lazy loading so what I want to happen is when an object fetches its child objects it uses the stored procedures already written. (The select procedures take into account an 'IsDeleted' flag, and use the 'ORDER BY' clause, amongst others)

I see from this article http://weblogs.asp.net/scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx that Linq to SQL allows drag and drop of SPs, which sounds, more or less, exactly what I want.

I've also come across the term DefiningQuery. http://msdn.microsoft.com/en-us/library/cc982038.aspx Is this what I want? I don't like the note 'Any changes made to the storage model, including defining queries, will be overwritten when you run the Update Model Wizard.'

In summary, what I want to happen is when an object fetches its child objects it uses my stored procedures.

Can I achieve my goal using Entity Framework? Have I missed something obvious?

Or should I try to be really clever and modify the db Entity T4 template, so that, for example, my generated Address model has this property:

public virtual ICollection<AddressLine> AddressLines { 
    get{
        DBWrapper _db = new DBWrapper();
        return _db.GetAddressLines(AddressID);
    } 
    set{}; 
}

where GetAddressLines is custom function that calls a function import and does the neccessary conversions.

like image 464
Mark_Gibson Avatar asked Oct 31 '11 12:10

Mark_Gibson


People also ask

Can we do SELECT on stored procedure?

You can run the following SELECT statement with the help of OPENROWSET and execute the stored procedure. Yes, indeed it is that easy. When you run the SELECT statement with OPENROWSET and if you get an error about ad hoc queries, you can run the following command to fix the error.

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.

How do I add a stored procedure to EDMX?

Add stored procedure in EDM: As we added tables in the EDM, the same way you can add stored procedures in the EDM. If you want to add it in existing EDM the right click on designer and click on "Update model from database..". This will popup update wizard where you can select stored procedures and add it.


1 Answers

It is not possible. You can import your stored procedures as function imports and manually execute them but you cannot replace queries generated by EF with custom stored procedures.

like image 114
Ladislav Mrnka Avatar answered Nov 28 '22 02:11

Ladislav Mrnka