In this article you will learn how to use Stored Procedure in Entity Framework MVC. Here are the steps:
Open Visual Studio, Add, then New Project,


Here is my Data Table from which I will show data using Stored Procedure.

Data in my Table.

The following is my Stored Procedure:
- USE [CompanyDB]
- GO
- /****** Object: StoredProcedure
- [dbo].[SearchEmployee]
- Script Date: 03/25/2016 18:48:48 ******/
- SETANSI_NULLSON
- GO
- SETQUOTED_IDENTIFIERON
- GO
-
- ALTERPROCEDURE [dbo].[SearchEmployee]
- AS
-
- SELECT*FROM Emp_Information
Now time to add ADO.NET Entity Data Model. So, right click on
Models folder, click Add, then
New Item.

Select your table and Stored Procedure here.


Right Click and then click on
Model Browser:

Right Click on your Stored Procedure, then click on
Add Function Import,

Here give name to your function and select your entities.

Now Add a Class.


And write below code here in this class:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace SP_EntityFramework_MVC.Models
- {
- public class Emp_Model
- {
- CompanyDBEntitiesempdb = newCompanyDBEntities();
- public List < Emp_Information > GetEmployees()
- {
- return empdb.FN_SearchEmployee().ToList();
- }
- }
- }
Now time to add a controller:

And write the following code in your controller:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using SP_EntityFramework_MVC.Models;
- namespace SP_EntityFramework_MVC.Controllers
- {
- public class EmployeeController: Controller
- {
- Emp_Model mod = newEmp_Model();
- public ActionResult Index()
- {
- List < Emp_Information > result = mod.GetEmployees();
- return View(result);
- }
- }
- }
Now Add View to your controller Index Action Method:
View.cshtml:
- @model IEnumerable<SP_EntityFramework_MVC.Models.Emp_Information>
- @{
- Layout = null;
- }
- <!DOCTYPE html>
- <html>
- <head>
- <meta name="viewport"content="width=device-width"/>
- <title>Employee List</title>
- </head>
- <body>
- <table class="table"style="border:solid4pxred; padding:20px; color:white; width:100%;">
- <tr><td align="center"colspan="5"style="border:solid4pxred; background-color:orange; padding:10px; font-family:Verdana; font-size:20pt; color:white; width:100%;"> Using Stored Procedure In Entity Framework MVC</td></tr>
- <tr style="background-color:green;">
- <th>
- @Html.DisplayNameFor(model =>model.EMP_ID)
- </th>
- <th>
- @Html.DisplayNameFor(model =>model.Name)
- </th>
- <th>
- @Html.DisplayNameFor(model =>model.ProjectName)
- </th>
- <th>
- @Html.DisplayNameFor(model =>model.ManagerName)
- </th>
- <th>
- @Html.DisplayNameFor(model =>model.City)
- </th>
- </tr>
- @foreach (var item in Model)
- {
- <tr style="background-color:skyblue; color:red; padding:10px;">
- <td>
- @Html.DisplayFor(modelItem =>item.EMP_ID)
- </td>
- <td>
- @Html.DisplayFor(modelItem =>item.Name)
- </td>
- <td>
- @Html.DisplayFor(modelItem =>item.ProjectName)
- </td>
- <td>
- @Html.DisplayFor(modelItem =>item.ManagerName)
- </td>
- <td>
- @Html.DisplayFor(modelItem =>item.City)
- </td>
- </tr>
- }
- </table>
- </body>
- </html>
Now run your application:
Read more articles on MVC:
DavePosted Jun 28, 2020, 1:34 PM
What if you wish to output extra fields NOT in the table? Why does EF always need the stored procedure's output columns to match a table?
shanthan rubanPosted Jul 24, 2019, 11:27 PM
Thank you.. really useful
vaibhav wadekarPosted Dec 6, 2018, 11:33 AM
How to play songs within mvc
Md RaquibPosted Nov 26, 2018, 1:37 PM
This is not working code, There is error in this. db.FN_SearchEmployee().ToList(); code generating error.
Jhon HernándezPosted Jun 12, 2018, 4:54 PM
Ah... of course... I don?t have a Data base
Jhon HernándezPosted Jun 12, 2018, 4:53 PM
On public partial class CompanyDBEntities : DbContext
Jhon HernándezPosted Jun 12, 2018, 4:53 PM
Hi, I?m using VS 2013 and this error comes from your code: An exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
Delpin Susai RajPosted Aug 28, 2016, 6:35 AM
Good one
Rahul Kumar SaxenaPosted Apr 8, 2016, 8:00 AM
Thanks Suraj Sahoo bro
Suraj SahooPosted Apr 7, 2016, 2:21 PM
Nice one Sir. :)
Rahul Kumar SaxenaPosted Apr 7, 2016, 1:37 PM
Thanks
Rahul Kumar SaxenaPosted Apr 7, 2016, 1:35 PM
Thanks Mohammed Ibrahim
Mohammed IbrahimPosted Apr 7, 2016, 12:47 PM
nice
Rahul Kumar SaxenaPosted Apr 7, 2016, 12:37 PM
Thanks Debasis Saha
Rahul Kumar SaxenaPosted Apr 7, 2016, 12:37 PM
Thanks Vignesh Mani
Rahul Kumar SaxenaPosted Apr 7, 2016, 12:36 PM
Thanks Shridhar Sharma
Debasis SahaPosted Apr 7, 2016, 10:25 AM
Nice One..
Vignesh ManiPosted Apr 7, 2016, 6:11 AM
Nice one
Shridhar SharmaPosted Apr 7, 2016, 5:46 AM
very well explained sir.
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:33 AM
Thanks 2 All
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:33 AM
Thanks Imtiyaz Ansari
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:33 AM
Thanks Karthikeyan K
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:33 AM
Thanks Mohammad Khalid
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:32 AM
Thanks Gowtham Rajamanickam
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:32 AM
Thanks Jaipal Reddy
Rahul Kumar SaxenaPosted Apr 7, 2016, 4:32 AM
Thanks Debendra Dash
Imtiyaz AnsariPosted Apr 7, 2016, 4:30 AM
Nice...
Karthikeyan KPosted Apr 7, 2016, 3:59 AM
Good one sir..Thanks for sharing
Mohammad KhalidPosted Apr 7, 2016, 3:19 AM
Clear steps followed. Very nice
Gowtham RajamanickamPosted Apr 7, 2016, 2:58 AM
good one..
Jaipal ReddyPosted Apr 7, 2016, 2:12 AM
Nice one sir. .
Debendra DashPosted Apr 7, 2016, 1:54 AM
Nice one sir.....