Entity Framework 4 - Calling a Select / Paging Stored Procedure
I'm just getting started with Entity Framework 4.0 and ASP.NET MVC 2 and have a few questions regarding using stored procedures and paging.
You can map the Insert, Update and Delete actions to stored procedures and I have already done that. However, for my search call, I need to display the Select action.
This is now the only / best way to do it by going to my Model Browser, right clicking on the Stored Procedure and Add Import Function and adding it.
This leads to the following code ...
var contactFormSubmissions = _entities.ContactFormSubmission_GetContactFormSubmissions(1, 10);
My problem is that it adds it to the global entity container at the root level and not to the ContactFormSubmission object like the Insert / Update and Delete actions.
I would prefer something like this, but via a stored procedure ...
_entities.ContactFormSubmissions.Select<ContactFormSubmission>(string.Empty, pageParam, pageSizeParam);
This way the selection is called in the same way as other actions, and I don't get a lot of functionality in the root of the Entity container, which can become unmanaged.
This is a less important issue, since at least all of her work is now.
My next question is how best to implement paging with this feature.
All the examples I've seen on how to swap using MVC and the Entity Framework have used LINQ and IQueryable. Is there a way to use IQueryable with lazy loading and LINQ Skip / Take functions using table based / sproc function?
http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/
PS - Any examples of paging in MVC with Entity Framework using SP would be great!
LINQ won't store a stored procedure out of the box. Entity Framework functions are the only way to call stored procedures through EF and return an object. This is understandable since EF knows to know which parameter displays your swap variables.
You would / shouldn't be doing this, but I feel compelled (since nothing is impossible) to say that you can write a custom LINQ implementation for your procedures, but that would be a mountain of work to cleanly wrap a couple of procedures.
Some suggestions:
- You can just use linq and skip the stored procedure
- If you are left with EF certifying a stored procedure with a function, you can fix many of the problems with functions by breaking your model into multiple contexts.
- You can use the EF context connection property to call the stored procedure directly, and use the viewmodel class to read (not part of EF) for use for display purposes. The id (or other primary key) field will still be the correct value to navigate to the detail screen or json callback to get an editable version of the real EF object.
None of them are perfect, but there are many ways to deal with it.