stored-procedurespaginationasp.net-mvc-2entity-framework-4

Entity Framework 4 - Calling a Select / Paging Stored Procedure


I am just starting out with the Entity Framework 4.0 and ASP.NET MVC 2 and have a few questions regarding the use of stored procedures and paging.

You can map the Insert, Update, and Delete actions to stored procedures and I have already done this. However for my paging to work I need to map the Select action.

Now is the only / best way I can do this by going to my model browser, right clicking on the stored procedure and "Add Function Import" and adding it.

This results in the following code....

var contactFormSubmissions = _entities.ContactFormSubmission_GetContactFormSubmissions(1, 10);

My issue with this is that it adds it to the global entity container at root level rather than the ContactFormSubmission entity like the Insert / Update and Delete actions.

I would rather something like this but through through a stored procedure...

_entities.ContactFormSubmissions.Select<ContactFormSubmission>(string.Empty, pageParam, pageSizeParam);

This way the select is called in the same way as the other actions and I dont end up with lots of functions in the root of the Entity Container which could get unmanagable.

This is a lesser issue as at least its all working currently.

My next question is how to best implement paging using this function.

All of the examples I have seen on how to accomplish paging using MVC and the Entity Framework have used LINQ and IQueryable. Is there a way of using IQueryable with lazy loading and the LINQ functions Skip / Take with a table based function /sproc?

http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

PS - any examples of paging in MVC with the Entity Framework using SP's would be great!


Solution

  • LINQ out of the box will not wrap a stored procedure. Entity Framework functions are the only way to call stored procedures via EF and return an entity. It's understandable, as EF has know what of knowing what parameter to map your paging variables too.

    You would/should never do this, but I feel compelled (since nothing is impossible) to say you could write a custom implementation of LINQ for your procedures, but that would be a mountain of work to cleanly wrap a couple of procedures.

    Some suggestions:

    None of these are perfect, but lots of ways to deal with this.