Working with Stored Procedure in Entity Framework Core (2024)

Working with Stored Procedure in Entity Framework Core (1)

Previous

Next

Here, you will learn how to execute the database stored procedure in Entity Framework Core.

EF Core provides the following methods to execute a stored procedure:

  1. DbSet<TEntity>.FromSql()
  2. DbContext.Database.ExecuteSqlCommand()

There are some limitations on the execution of database stored procedures using FromSql or ExecuteSqlCommand methods in EF Core2:

  1. Result must be an entity type. This means that a stored procedure must return all the columns of the corresponding table of an entity.
  2. Result cannot contain related data. This means that a stored procedure cannot perform JOINs to formulate the result.
  3. Insert, Update and Delete procedures cannot be mapped with the entity, so the SaveChanges method cannot call stored procedures for CUD operations.

Let's create our stored procedure in MS SQL Server before we execute it in EF Core.

If you follow the database-first approach, then execute the following script in your local SQL Server database:

USE [SchoolDB]GOSET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE PROCEDURE [dbo].[GetStudents] @FirstName varchar(50) AS BEGIN SET NOCOUNT ON; select * from Students where FirstName like @FirstName +'%' ENDGO

If you are following the code-first approach, then follow the below steps:

1. Add an empty migration by executing the following command in NPM (NuGet Package Manager):

PM> Add-migration sp-GetStudents

2. Write the following code in the Up method of empty migration class in <DateTime>_sp-GetStudents.cs:

public partial class spGetStudents : Migration{ protected override void Up(MigrationBuilder migrationBuilder) { var sp = @"CREATE PROCEDURE [dbo].[GetStudents] @FirstName varchar(50) AS BEGIN SET NOCOUNT ON; select * from Students where FirstName like @FirstName +'%' END"; migrationBuilder.Sql(sp); } protected override void Down(MigrationBuilder migrationBuilder) { }}

3. Now, create the above stored procedure in the database by executing the following command in NPM:

PM> Update-database

This will create the GetStudents stored procedure in the SQL Server database.

Execute Stored Procedures using FromSql

As mentioned in the previous chapter, the FromSql method of DbSet can be used to execute the raw SQL queries to the underlying database.In the same way, it can be used to execute the stored procedure which returns entity data, but with some limitations.

In the database, we can execute the GetStudents stored procedure with an INPUT parameter value like below:

GetStudents "Bill"-- orexec GetStudents "Bill"

You can execute SP using FromSql method in EF Core in the same way as above, as shown below.

var context = new SchoolContext(); var students = context.Students.FromSql("GetStudents 'Bill'").ToList();

You can also pass a parameter value using C# string interpolation syntax, as shown below.

var name = "Bill";var context = new SchoolContext(); var students = context.Students .FromSql($"GetStudents {name}") .ToList();//or//var students = context.Students.FromSql($"exec GetStudents {name}").ToList();

Use SqlParameter instance to specify the value of IN or OUT parameters as below:

var context = new SchoolContext(); var param = new SqlParameter("@FirstName", "Bill");//or/*var param = new SqlParameter() { ParameterName = "@FirstName", SqlDbType = System.Data.SqlDbType.VarChar, Direction = System.Data.ParameterDirection.Input, Size = 50, Value = "Bill"};*/var students = context.Students.FromSql("GetStudents @FirstName", param).ToList();

You can also specify @p0 for the first parameter, @p1 for the second, and so on.

var context = new SchoolContext(); var students = context.Students.FromSql("GetStudents @p0","Bill").ToList();

In the above example, @p0 is used for the first parameter because named parameters are not supported yet in EF Core.

Note: All entities in the result will be tracked by the DbContext by default.If you execute the same stored procedure with the same parameters multiple times, then it will execute the same SQL statement each time, but it will only track one result set.For example, the following example will execute the GetStudents stored procedure three times, but it will cache and track only one copy of the result.

var context = new SchoolContext(); var list1 = context.Students.FromSql("GetStudents 'Bill'").ToList();var list2 = context.Students.FromSql("GetStudents 'Bill'").ToList();var list3 = context.Students.FromSql("GetStudents 'Bill'").ToList();

Execute Stored Procedure using ExecuteSqlCommand()

The ExecuteSqlCommand() method is used to execute database commands as a string.It returns an integer for the number of rows was affected through the specified command.

var context = new SchoolContext(); var rowsAffected = context.Database.ExecuteSqlCommand("Update Students set FirstName = 'Bill' where StudentId = 1;");

In the above example, the update command is passed in the ExecuteSqlCommand method.The value of rowsAffected will be 1 because only 1 row affected with the specified update command.

In the same way, we can execute stored procedures for create, update and delete commands.Consider the following stored procedure which inserts a record in the Students table in the database:

CREATE PROCEDURE CreateStudent @FirstName Varchar(50), @LastName Varchar(50)ASBEGIN SET NOCOUNT ON; Insert into Students( [FirstName] ,[LastName] ) Values (@FirstName, @LastName)ENDGO

Now, you can execute the above SP as below.

var context = new SchoolContext(); context.Database.ExecuteSqlCommand("CreateStudents @p0, @p1", parameters: new[] { "Bill", "Gates" });

In the same way, you can execute stored procedures for Update and Delete commands.

Previous

Next

Working with Stored Procedure in Entity Framework Core (2024)
Top Articles
5 Ways Students Can Address You Besides "Mr." or "Ms."
How Hard Are The A Level Exams? | TeachTutti
Devin Mansen Obituary
Www.paystubportal.com/7-11 Login
Asist Liberty
What spices do Germans cook with?
Readyset Ochsner.org
Google Jobs Denver
50 Meowbahh Fun Facts: Net Worth, Age, Birthday, Face Reveal, YouTube Earnings, Girlfriend, Doxxed, Discord, Fanart, TikTok, Instagram, Etc
7543460065
Cape Cod | P Town beach
Tiger Island Hunting Club
Hallelu-JaH - Psalm 119 - inleiding
Unit 1 Lesson 5 Practice Problems Answer Key
Taylor Swift Seating Chart Nashville
Jasmine Put A Ring On It Age
Walthampatch
People Portal Loma Linda
Guilford County | NCpedia
How to find cash from balance sheet?
Are They Not Beautiful Wowhead
Q33 Bus Schedule Pdf
Richland Ecampus
/Www.usps.com/International/Passports.htm
The best firm mattress 2024, approved by sleep experts
Touchless Car Wash Schaumburg
Morse Road Bmv Hours
Dark Entreaty Ffxiv
Ihub Fnma Message Board
What Individuals Need to Know When Raising Money for a Charitable Cause
Tuw Academic Calendar
Geico Car Insurance Review 2024
R/Airforcerecruits
Cable Cove Whale Watching
Obituaries, 2001 | El Paso County, TXGenWeb
Log in or sign up to view
Why comparing against exchange rates from Google is wrong
The Ultimate Guide to Obtaining Bark in Conan Exiles: Tips and Tricks for the Best Results
Palmadise Rv Lot
Indiana Immediate Care.webpay.md
Colorado Parks And Wildlife Reissue List
Tsbarbiespanishxxl
Miami Vice turns 40: A look back at the iconic series
COVID-19/Coronavirus Assistance Programs | FindHelp.org
Catchvideo Chrome Extension
Keci News
Zom 100 Mbti
Wpne Tv Schedule
Compete My Workforce
Morgan State University Receives $20.9 Million NIH/NIMHD Grant to Expand Groundbreaking Research on Urban Health Disparities
Cheryl Mchenry Retirement
Latest Posts
Article information

Author: Reed Wilderman

Last Updated:

Views: 5697

Rating: 4.1 / 5 (52 voted)

Reviews: 91% of readers found this page helpful

Author information

Name: Reed Wilderman

Birthday: 1992-06-14

Address: 998 Estell Village, Lake Oscarberg, SD 48713-6877

Phone: +21813267449721

Job: Technology Engineer

Hobby: Swimming, Do it yourself, Beekeeping, Lapidary, Cosplaying, Hiking, Graffiti

Introduction: My name is Reed Wilderman, I am a faithful, bright, lucky, adventurous, lively, rich, vast person who loves writing and wants to share my knowledge and understanding with you.