How to access stored procedure in Entity Framework

First we have to create stored procedure using MS SQL Server 2008 R2

CREATE PROCEDURE dbo.SelectCategories
AS
BEGIN
	SELECT * FROM dbo.Articles;
END

 

From your Model(Model.edmx), let's right click in your model designer then Add -> Function Import.
This is what it looks like when you add Function Import.

Press Ok then Save and Build your project.

You can now access your stored procedure using EF4

Public ActionResult Articles(){
   ModelEntities _db = new ModelEntities ();
   var model = _db.SelectArticles();
   return View(model);
}

Happy Coding ^_^

blog comments powered by Disqus