DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Stack in Data Structures
  • IDE Changing as Fast as Cloud Native
  • Auditing Tools for Kubernetes
  • How to Supplement SharePoint Site Drive Security With Java Code Examples

Trending

  • Stack in Data Structures
  • IDE Changing as Fast as Cloud Native
  • Auditing Tools for Kubernetes
  • How to Supplement SharePoint Site Drive Security With Java Code Examples
  1. DZone
  2. Coding
  3. Frameworks
  4. How to Use Stored Procedure With Entity Framework Code First

How to Use Stored Procedure With Entity Framework Code First

In this post we're going to learn about how we can use stored procedure with Entity framework code first.

Jalpesh Vadgama user avatar by
Jalpesh Vadgama
·
Nov. 30, 15 · Tutorial
Like (2)
Save
Tweet
Share
9.93K Views

Join the DZone community and get the full member experience.

Join For Free

I'm getting lots of request from the readers of my blog about writing a post on how to use stored procedure with Entity Framework Code First. So, in this post we're going to do just that.

To demonstrate, we are going to create a table called Employee as following.

table-entity-framework-code-first-stored-procedure

Here is the create table script for the same.

CREATETABLE[dbo].[Employee]([EmployeeId] [int] NOTNULL,[FirstName] [nvarchar](50) NULL,[LastName] [nvarchar](50) NULL,[Designation] [nvarchar](50) NULL,CONSTRAINT[PK_Employee] PRIMARYKEYCLUSTERED ([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]

Following is the data I have inserted there.

entity-framework-table-data

And here is the store procedure we are using for that.

CREATEPROCEDUREusp_GetAllEmployeesASSELECTEmployeeId,FirstName,LastName,Designation FROMEmployee

Now that we are already done with our database side, it's time to write some C# code. I'm going to use the 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.

namespaceCodeFirstStoredProcedure{publicclassEmployee{publicintEmployeeId { get; set; }publicstringFirstName { get; set; }publicstringLastName { get; set; }publicstringDesignation { get; set; }}}

And then, I created Entity Framework dbcontext as following.

usingSystem.Data.Entity;   namespaceCodeFirstStoredProcedure{publicclassEmployeeDbContext : DbContext{publicEmployeeDbContext(): base("EmployeeConnectionString"){   }publicDbSet<Employee> Employees { get; set; }}}

The following is a code for using stored procedure to get data from the database via Entity Framework Code First.

usingSystem;usingSystem.Linq;   namespaceCodeFirstStoredProcedure{classProgram{staticvoidMain(string[] args){using(EmployeeDbContext dbContext = newEmployeeDbContext()){stringcommandText = "[dbo].[usp_GetAllEmployees]";var employees = dbContext.Database.SqlQuery<Employee>(commandText).ToList();   Console.WriteLine("Printing Employee");foreach(var employee inemployees){Console.WriteLine(employee.EmployeeId);Console.WriteLine(employee.FirstName);Console.WriteLine(employee.LastName);Console.WriteLine(employee.Designation);Console.WriteLine("-----------------");}}Console.ReadKey();}}}

In the above code, if you look carefully, I have used SQL Query function to execute stored procedure which will return a list of employees. Then, I have printed this list with console.writeline.  Now, when you run this application, the 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 the following location: https://github.com/dotnetjalps/EFCodeFirstStoredProcedure

Entity Framework Database Framework

Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Stack in Data Structures
  • IDE Changing as Fast as Cloud Native
  • Auditing Tools for Kubernetes
  • How to Supplement SharePoint Site Drive Security With Java Code Examples

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: