Friday, November 6, 2015

How to use stored procedure with Entity Framework Code First

I'm getting lots of request from readers of my blog about writing blog post about how to use stored procedure with entity framework code first. So in this blog post we're going to learn about how we can use stored procedure with Entity framework code first.

To demonstrate that we are going to use create a table called Employee like following.

table-entity-framework-code-first-stored-procedure
And here is the create table script for the same.
CREATE TABLE [dbo].[Employee](
[EmployeeId] [int] NOT NULL,
[FirstName] [nvarchar](50) NULL,
[LastName] [nvarchar](50) NULL,
[Designation] [nvarchar](50) NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED 
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

And following is the data I have inserted there.

entity-framework-table-data

And here is the store procedure we are using for that.
CREATE PROCEDURE usp_GetAllEmployees
AS
SELECT EmployeeId,FirstName,LastName,Designation FROM Employee

Now we have already done with our database side, it's time to write some C# code. I'm going to use simple console application for the same.

console-application-ef-code-first-stored-procedure

Now It's time to add entity framework via nuget package .

nuget-package-entity-framework-code-first

Here is the model class I have created.
namespace CodeFirstStoredProcedure
{
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Designation { get; set; }
}
}
And then I have created entity framework dbcontext like following.
using System.Data.Entity;

namespace CodeFirstStoredProcedure
{
public class EmployeeDbContext : DbContext
{
public EmployeeDbContext()
: base("EmployeeConnectionString")
{

}
public DbSet<Employee> Employees { get; set; }
}
}
And following is a code for using stored procedure to get data from database via entity framework code first.
using System;
using System.Linq;

namespace CodeFirstStoredProcedure
{
class Program
{
static void Main(string[] args)
{
using (EmployeeDbContext dbContext = new EmployeeDbContext())
{
string commandText = "[dbo].[usp_GetAllEmployees]";
var employees = dbContext.Database.SqlQuery<Employee>(commandText).ToList();

Console.WriteLine("Printing Employee");
foreach (var employee in employees)
{
Console.WriteLine(employee.EmployeeId);
Console.WriteLine(employee.FirstName);
Console.WriteLine(employee.LastName);
Console.WriteLine(employee.Designation);
Console.WriteLine("-----------------");
}
}
Console.ReadKey();
}
}
}

Here in the above code, If you see care fully. I have used SQL Query function to execute stored procedure and that will return a list of employee. Then I have printed this list with console.writeline.  Now when you run this application, output will be as expected.

entity-framework-code-first-stored-procedure

That's it. It's very easy to use stored procedure with entity framework code first. Hope you like it. Stay tuned for more!!.
You can find complete source code of this sample application at - following location - https://github.com/dotnetjalps/EFCodeFirstStoredProcedure
Share:

0 comments:

Post a Comment

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews