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 : 25144 | 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.


Sheo Narayan


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

hi.. I request to follow these links for your solution.

Sr. Software Engineer

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

Login to post response