Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Using Stored Procedure in Entity Framework MVC

Article:
Viewed:  1041 
Posted On:  3/25/2016 5:32:02 AM 
How we can use Stored Procedure in Entity Framework in a MVC application? 

In this article I am going to show how we can use stored procedure in Entity Framework with MVC Application.

Open Visual Studio -> Add -> New Project

1.png

Image 1.

2.png

Image 2.

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

3.png

Image 3.

Data in my Table.

4.png

Image 4.

Below is my Stored Procedure:

5.png

Image 5.

USE [CompanyDB]
GO
/****** Object:  StoredProcedure
 [dbo].[SearchEmployee]    
Script Date: 03/25/2016 18:48:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
 
ALTER PROCEDURE [dbo].[SearchEmployee]
AS
 
SELECT * FROM Emp_Information

 

Now Time to Add ADO.NET Entity Data Model. So Right click on Model Folder-> Add New Item.

 

6.png

Image 6.

7.png

Image 7.

8.png

Image 8.

9.png

Image 9.

10.png

Image 10.

Select your table and Stored Procedure here.

11.png

Image 11.

12.png

Image 12.

Right Click -> And Click on Model Browser:

13.png

Image 13.

Right Click on your Stored Procedure -> Click on Add Function Import

14.png

Image 14.

Here give name to your function and select your entities.

15.png

Image 15.

Now Add a Class.

16.png

Image 16.

17.png

Image 17.

And write below code here in this class:

18.png

Image 18.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
 
namespace SP_EntityFramework_MVC.Models
{
    public class Emp_Model
    {
        CompanyDBEntities empdb = new CompanyDBEntities();
 
        public List<Emp_Information> GetEmployees()
        {
            return empdb.FN_SearchEmployee().ToList();
        }
    }
}

Now time to add a controller:


19.png

Image 19.

20.png

Image 20.

21.png

Image 21.

And do below code in your controller:

22.png

Image 22.

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 = new Emp_Model();
 
        public ActionResult Index()
        {
            List<Emp_Information> result = mod.GetEmployees();
            return View(result);
        }
    }
}
 

Now Add View to your controller Index Action Method:

23.png

Image 23.

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:solid 4px red; padding:20px; color:white; width:100%;">
        <tr><td align="center" colspan="5" style="border:solid 4px red; 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:

24.png

Image 24.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us