Creating Stored Procedures With Entity Framerwork

This article explains how to create Stored Procedures with Entity Framerwork. I do not want to use the ToList() method, I just want to use my own methods. Let's start.

Step 1: First of all open SQL Server 2008 R2 (for me) and I will use the Northwind database that you are familiar with.

Provide your procedure name and then write your query. Execute your query (F5), the result is that the command(s) were completed successfully.

Entity-Framerwork-1.jpg
 
Is it perfect? (Ha:)) Now refresh the Programmability folder then click "Toggle"; here is the Stored Procedure.

Entity-Framerwork-2.jpg

Step 2: Now open your Visual Studio 2012 and select "File" >> "New" >> "Project..." >> "Windows Forms Application" (the project name is "UsingStoreProc"). Click the "OK" button.
 
Note : If you have no idea of how create a new Entity Framework structure you should visit this page:

http://www.c-sharpcorner.com/UploadFile/4923b7/creating-a-edmx-file-using-entity-data-model/

Entity-Framerwork-3.jpg
 
Drag the DataGridView and set the dock in parent container.

Entity-Framerwork-4.jpg
 
Step 3: Create  a new Entity Framework connection.

Entity-Framerwork-5.jpg

Choose your Entity Model. Generate from database or Empty Model. I should use the first one.

Entity-Framerwork-6.jpg

Click the "Next" button.
 
The next screen will display the connection settings.

Entity-Framerwork-7.jpg

Description

Server Name: The default server name is "sa". We can say system admin.

Log on to the server: This is your security properties. How to log on your database server?

Connect to the database: Select your database from here.

Test Connection: If you have a problem about the connection then this button will say that something is wrong.
 
And then click the "Ok" button.
 
That provides all the settings for your connection string, including the database name, security settings and so on.

Entity-Framerwork-8.jpg
  
Click the "Next" button.

Now this page is really great. Click the Stored Procedures and Functions.

Entity-Framerwork-9.jpg
 
Now  choose the Stored Procedure name. Oh there is the CategoryList method. Go on.

Entity-Framerwork-10.jpg
 
Open your Windows Forms application and click the form_load.

Entity-Framerwork-11.jpg

CategoryList() is a method. Execute the project; the result is:

Entity-Framerwork-12.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all