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.
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.
Here is the create table script for the same.
Following is the data I have inserted there.
And here is the store procedure we are using for that.
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.
Now, it's time to add Entity Framework via nuget package .
Here is the model class I have created.
And then, I created Entity Framework dbcontext as following.
The following is a code for using stored procedure to get data from the database via Entity Framework Code First.
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.
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
Published at DZone with permission of Jalpesh Vadgama, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
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