Entity Framework Stored Procedures - Microsoft Entity Framework

What is Entity Framework Stored Procedures?

The Entity Framework permits you to use stored procedures in the Entity Data Model in its place of, or in permutation with, its automatic command group.

  • You can use stored procedures to execute predefined logic on database tables, and many administrations have rules in place that require the use of these stored procedures.
  • It can also state that EF should use your stored procedures for inserting, updating, or deleting entities.
  • Although the animatedly built commands are secure, efficient, and generally as good as or better than those you may write yourself, there are many cases where stored procedures already exist and your company performs may restrict direct use of the tables.
  • Otherwise, you may just want to have explicit control over what is executed on the store and prefer to create stored procedures.

The following instance creates a new project from File → new → Project.
procedure_new_project

Step 1 − Select the Console Application from the middle panel and enter StoredProceduresDemo in the name field.

Step 2 − In Server explorer right-click on your database.

Step 3 − Select New Query and enter the following code in T-SQL editor to add a new table in your database.

Step 4 − Right-click on the editor and select Finish.

editor

Step 5 − Right-click on your database and click refresh. You will see the newly added table in your database.

Step 6 − In Server explorer, right-click on your database again.
server_database

Step 7 − Select New Query and enter the following code in T-SQL corrector to add a stored procedure in your database, which will profit the Student grades.

Step 8 − Right-click on the editor and select Execute.

execute

Step 9 − Right-click on your database and click refresh. You will see that a kept procedure is formed in your database.

store_procedure_created

Step 10 − Right-click on the project name in Solution Explorer and select Add → New Item.

Step 11 − Then select ADO.NET Entity Data Model in the Templates panel.
template_pane

Step 12 − Enter Spode as name, and then click Add.

Step 13 − In the Select Model Substances dialog box, select EF designer from database, and then click Next.
model_contents.jpg

Step 14 − Select your database and click Next.
database_1.jpg

Step 15 − In the Select Your Database Objects dialog box click on tables, views.
database_objects.jpg

Step 16 − Select the GetStudentGradesForCourse function located under the Stored Procedures and Functions node and click Finish.

Step 17 − Select View → Other Windows → Entity Data Model Browser and right-click GetStudentGrades under Function Imports and select Edit.
entity_browser

It will produce the following conversation.

entity_browser_dialog.jpg

Step 18 − Click on Entities radio button and select Student Grade from the combo box as yield type of this stored procedure and click Ok.

Let’s take a look at the following C# code in which all the grades will be saved by passing the student ID as parameter in GetStudentGrades stored procedure.

When the above code is collected and performed you will receive the following output –

We recommend that you execute the above instance in a step-by-step method for better considerate.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Microsoft Entity Framework Topics