Call Stored Procedure from Entity Framework

342
Views
1
Answers

How do I call stored procedure using Entity Framework. The stored procedure names are not showing up in the context. Am I missing something?

edit | flag
William Patrick
Asked on: Jan 20, 2012 at 9:59PM

1 Answers

101
5
101

You need to do Function Import to use the stored procedure using Entity framework.

Follow the steps to call a stored procedure

  1. Open Entity Framework Model page.
  2. Right click on blank area and choose Add -> Function Import
  3. In Add Function Import dialog, select the stored procedure you need 'Productsp' in this case, give function name 'Some name' and choose return type.
  4. If your stored procedure is return multiple record then you have to create one custom entity
  5. To create the custom entity, right click on Entity Framework Model page and choose Add -> Entity
  6. You can also choose the existing Entities from the list.
  7. Click on Ok button to add the function in Entity Framework
  8. In your page, Create instance of entities class and call your function with proper parameter value like

)

 public ActionResult Products(int Productid)
   
{NorthwindEntities dbContext = new NorthwindEntities();
       
var cities = from c in dbContext.Productsp(10)

                     
select c;

       
return View();
   
}
edit | flag
Gokul A
Answered on: Jan 20, 2012 at 10:26PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags