How to call stored procedure using DbContext in ASP.NET MVC

Posted by Sheonarayan under Error and Solution on 10/19/2013 | Points: 10 | Views : 25525 | Status : [Administrator] | Replies : 1

Getting table data using DbContext in Entity Framework is easy, you just need to create a DbSet collection property something like this
public DbSet<PointModel> PointModels { get; set; }

But if you want to call the Stored Procedure then? Lets learn it.

To call the Stored Procedure using DbContext, there are many ways that you find using google, in my case all of them was not working. Some were like passing SqlParameter or by passing the parameter values separated by comma etc.

What worked is below.

 var cloud = db.Database.SqlQuery<TagCloud>("EXEC VideoTagCloud {0}", 0);


Here, VideoTagCloud is my stored procedure name, {0} is the first parameter place holder, similarly you can have n number of placeholder and values separated by comma.

Hope this will be helpful to someone.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com



Responses

Posted by: Samirbhogayta on: 10/21/2013 [Member] Starter | Points: 25

Up
0
Down
hi.. I request to follow these links for your solution.

http://stackoverflow.com/questions/12707682/how-to-map-a-stored-procedure-using-dbcontext-in-asp-net-mvc-3-entity-framework
http://www.entityframeworktutorial.net/EntityFramework4.3/execute-stored-procedure-using-dbcontext.aspx

SAMIR
Sr. Software Engineer

Sheonarayan, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response