In this article I will explain a simple tutorial with an example, how to call Stored Procedure using Entity Framework in ASP.Net MVC 5 Razor.
The Stored Procedure will be called by passing the parameter value using Entity Framework in ASP.Net MVC 5 Razor.
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
Stored Procedure
You will need to create the following Stored Procedure in the Northwind Database. The Stored Procedure accepts @ContactName parameter which is used to perform a search on the records in Customers Table.
CREATE PROCEDURE Customers_SearchCustomers
@ContactName NVARCHAR(30)
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Customers
WHERE ContactName LIKE '%' + @ContactName + '%'
END
Configuring and connecting Entity Framework to database
You will need to configure the Entity Framework in order to connect to the database.
Once the Entity Framework is configured, the next step is to import the Stored Procedure in the Entity Framework model. In order to do so, you will need to Right Click the Entity Model and select Update Model from Database option as shown below.
The above step will open the Update Wizard where you will need to select the Stored Procedure and click Finish.
Now we will need to import the Stored Procedure into the Entity Framework so that it can be called as a Function using Entity Framework.
Thus you will need to Right Click the Entity Model, click Add and then click Function Import.
This will open the Add Function Import dialog window. Here first you need to specify the Function Import Name which is the name of the function used to call the Stored Procedure and then select the Stored Procedure that will be executed when the function is called.
The Return Type is selected as Entities which is the Customer Entity class.
Controller
The Entity Framework is now configured and hence now we can create a Controller and write code to fetch the records from the Customers Table of the Northwind Database.
The Controller consists of two Action methods.
Action method for handling GET operation
Inside the Index Action method, the Stored Procedure is called using the SearchCustomers function created using the Function Import procedure done earlier.
The parameter ContactName is passed as empty string and hence it gets all records from the Customers table of the Northwind database.
Finally the list of Customers Entity is returned to the View.
Action method for handling POST operation
When the Form is submitted, the value of the Customer Name TextBox is submitted this Action method and the value is passed as parameter to the SearchCustomers function and the list of Customers Entity is returned to the View.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
NorthwindEntities entities = new NorthwindEntities();
return View(entities.SearchCustomers(""));
}
[HttpPost]
public ActionResult Index(string customerName)
{
NorthwindEntities entities = new NorthwindEntities();
return View(entities.SearchCustomers(customerName));
}
}
View
Inside the View, in the very first line the Customer Entity is declared as IEnumerable which specifies that it will be available as a Collection.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following parameters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.
FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the View, a TextBox is created using the Html.TextBox HTML Helper function and there is a Submit button which when clicked, the Form gets submitted.
For displaying the records, an HTML Table is used. A loop will be executed over the Model which will generate the HTML Table rows with the Customer records.
@model IEnumerable<Entity_Framework_Stored_Proc_MVC.Customer>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<span>Customer Name:</span> @Html.TextBox("CustomerName")
<input type="submit" value="Search"/>
<br/>
<br/>
<table cellpadding="0" cellspacing="0">
<tr>
<th>CustomerID</th>
<th>ContactName</th>
<th>City</th>
<th>Country</th>
</tr>
@foreach (Customer customer in Model)
{
<tr>
<td>@customer.CustomerID</td>
<td>@customer.ContactName</td>
<td>@customer.City</td>
<td>@customer.Country</td>
</tr>
}
</table>
}
</body>
</html>
Screenshots
Grid displaying all records
Grid displaying Filtered records
Downloads