If you want to know how to implement stored procedure in entity framework database, then, Here are the steps to implement the same.
We can also access stored procedures in a .net application like tables. These procedures are converted as c# methods.
At the time of adding stored procedures, Entity framework generates following items:
1. C# method: method name same as stored procedure name.
2. Result class: Name of the class is prepared as follows:
Syntax: Procedurename_results
Eg: getemployees_results
Steps to Implement
1. Create required stored procedure in the database.
2. Add stored procedure in a .net application using entity framework.
3. Invoke the stored procedure using c# methods with help of data context class object.
Eg: Create asp.net MVC application to display employee details by invoking a stored procedure from entity framework.
1. Create required stored procedure on SQL server
[php] Create procedure <g class="gr_ gr_42 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="42" data-gr-id="42">getemps</g> @dno int As Begin Select * from emp where deptno=@dno End [/php]
2.Create MVC Application (webapplication1)and add above-stored procedure by using Entity Framework.
3.Build the project.
HomeController.cs
[php] Using webapplication1.Models; Namespace webapplication1.controllers { Public class Homecontroller: Controller { Empdbentities db=new Empdbentities (); Public ActionResult Index() { List<Getemps_Result> emplist=db.Getemps(10).tolist(); return view(emplist); } } } [/php]
Index.cshtml:
[php] @using Webapplication1.Models; @model Ienumberable<Getemps_result> <h1>Employee Details</h1> <hr/> <table border=”2” width=”400”> <tr> <th>Ename</th> <th>Job</th> <th>sal</th> <th>Deptno</th> </tr> @{ Foreach(GetEmps_result item in Model) { <tr> <td>@item.Ename</td> <td>@item.job</td> <td>@item.sal</td> <td>@item.deptno</td> </tr> } } </table> [/php]