{"id":77475,"date":"2022-11-22T08:00:55","date_gmt":"2022-11-22T07:00:55","guid":{"rendered":"https:\/\/drafts.code-maze.com\/?p=77475"},"modified":"2022-12-02T09:48:27","modified_gmt":"2022-12-02T08:48:27","slug":"efcore-execute-stored-procedures","status":"publish","type":"post","link":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/","title":{"rendered":"How to Execute Stored Procedures With EF Core 7"},"content":{"rendered":"<p>In this article, we will see how to use stored procedures in Entity Framework Core 7.<\/p>\n<p>Although Entity Framework and LINQ provide a simple way to work with the data, stored procedures still have their place when considering data access and manipulation.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/dotnet-efcore\/ExecuteStoredProceduresInEFCore\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<h2>Setup<\/h2>\n<p>Let&#8217;s define our data model and stored procedures.\u00a0<\/p>\n<p>We will use the same structure as in <a href=\"https:\/\/code-maze.com\/ef-core-filtered-include\/\" target=\"_blank\" rel=\"noopener\">our previous\u00a0article<\/a>.\u00a0<\/p>\n<p>So we have a <code>Student<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Student\r\n{\r\n    public int Id { get; set; }\r\n    public string? Name { get; set; }\r\n    public int Mark { get; set; }\r\n\r\n    public int CourseId { get; set; }\r\n    public Course? Course { get; set; }\r\n}<\/pre>\n<p>And the <code>Course<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Course\r\n{\r\n    public int Id { get; set; }\r\n    public string? Title { get; set; }\r\n\r\n    public ICollection&lt;Student&gt;? Students { get; set; }\r\n}<\/pre>\n<p>We will also define our stored procedures.\u00a0<\/p>\n<p>First <code>FindStudents<\/code>, a procedure that returns a list of <code>Students<\/code> filtered by the name:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">CREATE OR ALTER PROCEDURE [dbo].[FindStudents]\r\n    @SearchFor NVARCHAR(50)\r\nAS\r\nBEGIN\r\n    SET NOCOUNT ON;\r\n    SELECT * FROM Students WHERE [Name] LIKE '%' + @SearchFor + '%'\r\nEND<\/pre>\n<p>Then <code>FindStudentsAlt<\/code>, a procedure that returns a list of students with only student names and course titles filtered by student name:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">CREATE OR ALTER PROCEDURE [dbo].[FindStudentsAlt]\r\n    @SearchFor NVARCHAR(50)\r\nAS\r\nBEGIN\r\n    SET NOCOUNT ON;\r\n    SELECT \r\n        StudentName = S.[Name],\r\n        CourseTitle = C.Title\r\n    FROM \r\n        Students S\r\n    LEFT JOIN Courses C ON S.CourseId = C.Id \r\n    WHERE \r\n        S.[Name] LIKE '%' + @SearchFor + '%'\r\nEND<\/pre>\n<p>We will also define a procedure to update student marks, <code>UpdateStudentMark<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">CREATE OR ALTER PROCEDURE UpdateStudentMark\r\n    @Id int,\r\n    @Mark int\r\nAS\r\nBEGIN\r\n    UPDATE \r\n        Students\r\n    SET \r\n        Mark = @Mark\r\n    WHERE \r\n        Id = @Id;\r\nEND<\/pre>\n<p>And finally, we will define a procedure that updates a student&#8217;s mark and returns an average mark for all students:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">CREATE OR ALTER PROCEDURE UpdateStudentMarkWithReturnValue\r\n    @Id int,\r\n    @Mark int,\r\n    @AvgMark int OUTPUT\r\nAS\r\nBEGIN\r\n    UPDATE \r\n        Students\r\n    SET \r\n        Mark = @Mark\r\n    WHERE \r\n        Id = @Id;\r\n\r\n    SELECT @AvgMark = AVG(Mark) FROM Students\r\nEND<\/pre>\n<p>We can see that we do not use <code>SET NOCOUNT ON;<\/code> in this procedure because we want to get the number of affected rows when we call this procedure.<\/p>\n<p>Using stored procedures can simplify and improve the execution of SQL queries. We can use stored procedures to define complex queries which might become too complicated to read and write in LINQ.\u00a0<\/p>\n<p>If we want to use a stored procedure to query the data, <strong>the query needs to return the complete set of properties (columns) for EF to map the data to the entity.<\/strong><\/p>\n<h2>Stored procedures in EF 7<\/h2>\n<p>We can divide methods to execute stored procedures in EF into two groups, methods to query the data and methods to add\/update\/delete data.<\/p>\n<p>If we want to query the data, we will use <code>FromSql<\/code>, <code>FromSqlInterpolated<\/code>, or <code>FromSqlRaw<\/code> methods that are available only on the <code>DbSet<\/code> object.<\/p>\n<p>To add, update or remove records, we need to use <code>ExecuteSql<\/code>, <code>ExecuteSqlInterpolated<\/code>, or <code>ExecuteSqlRaw<\/code> methods, which are available for <code>DbContext.Database<\/code> object.<\/p>\n<p>Let&#8217;s take a closer look at these methods.\u00a0<\/p>\n<h3>DbSet.FromSql<\/h3>\n<p>Let&#8217;s define a method <code>FindStudentsFromSql<\/code> that uses the <code>FromSql<\/code> method to call <code>FindStudents<\/code> stored procedure and returns a list of <code>Students<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static List&lt;Student&gt;? FindStudentsFromSql(AppDbContext context, string searchFor)\r\n{\r\n    return context?.Students?.FromSql($\"FindStudents {searchFor}\").ToList();\r\n}<\/pre>\n<p><code>FromSql<\/code> method is introduced in EF Core 7.0. It takes <code>FormattableString<\/code> as the only parameter, which means it is safe against SQL injection since all parameter data are wrapped in an <code>DbParameter<\/code> object. As a parameter string, we pass the name of the procedure and the value for the parameter.<\/p>\n<h3>DbSet.FromSqlInterpolated<\/h3>\n<p><code>FromSqlInterpolated<\/code> is s available since EF Core 3.0, and it is basically the same as <code>FromSql<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static List&lt;Student&gt;? FindStudentsFromSqlInterpolated(AppDbContext context, string searchFor)\r\n{\r\n    return context?.Students?.FromSqlInterpolated($\"FindStudents {searchFor}\").ToList();\r\n}<\/pre>\n<p>Next, when we call the\u00a0 <code>FindStudentsAlt<\/code> stored procedure, we cannot map the result to our data model:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static List&lt;Student&gt;? FindStudentsAltFromSqlInterpolated(AppDbContext context, string searchFor)\r\n{\r\n    return context?.Students?.FromSqlInterpolated($\"FindStudentsAlt {searchFor}\").ToList();\r\n}<\/pre>\n<p>As a result, we see it will throw an exception since the underlying stored procedure returns an incomplete set of results:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void WhenFindStudentsAltFromSqlInterpolated_ThenSuccess()\r\n{\r\n    Assert.Throws&lt;InvalidOperationException&gt;(\r\n        () =&gt; Methods.FindStudentsAltFromSqlInterpolated(context, \"100\")\r\n    );\r\n}<\/pre>\n<h3>DbSet.FromSqlRaw<\/h3>\n<p>Unlike <code>FromSql<\/code> and <code>FromSqlInterpolated<\/code>, <code>FromSqlRaw<\/code> <strong>is not safe from SQL injection attacks<\/strong>. Therefore, we need to pay extra attention when defining SQL commands.<\/p>\n<p><strong>We should never pass concatenated or interpolated strings into this method.<\/strong><\/p>\n<p>Let&#8217;s define how we can use it to call stored procedure:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static List? FindStudentsFromSqlRaw(AppDbContext context, string searchFor)\r\n{\r\n    return context?.Students?.FromSqlRaw(\"FindStudents @searchFor\",\r\n        new SqlParameter(\"@searchFor\", searchFor)).ToList();\r\n}\r\n<\/pre>\n<p>Let&#8217;s now show how we can misuse this method and, therefore, abuse it to perform SQL injection attacks.<\/p>\n<p>First, we need to define our unsafe method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static List&lt;Student&gt;? FindStudentsFromSqlRawUnsafe(AppDbContext context, string searchFor)\r\n{\r\n    return context?.Students?.FromSqlRaw($\"FindStudents @searchFor = '{searchFor}'\").ToList();\r\n}<\/pre>\n<p>Here we can see that we call the correct stored procedure, but the <code>@searchFor<\/code> parameter is not passed as a <code>DbParameter<\/code> and the SQL command is constructed using interpolated string.<\/p>\n<p>We can still use the method safely:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void WhenFindStudentsExecuteSqlRawUnsafe_ThenSuccess()\r\n{\r\n    var results = FindMethods.FindStudentsFromSqlRawUnsafe(context, \"100\");\r\n\r\n    Assert.True(results?.Count == 9);\r\n}\r\n<\/pre>\n<p>But we can also use it maliciously:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public void WhenFindStudentsExecuteSqlRawUnsafeSqlInjection_ThenSuccess()\r\n{\r\n    var results = FindMethods.FindStudentsFromSqlRawUnsafe(context, \r\n        @\"xyz'; UPDATE Students SET Name = 'Student 000' WHERE Id = 1; SELECT '\");\r\n\r\n    Assert.True(results?.Count == 0);\r\n}\r\n<\/pre>\n<p>This example will not return any Students, but it will execute an UPDATE (or any other) query and possibly compromise the whole database.<\/p>\n<h3>Database.ExecuteSql<\/h3>\n<p>As we already mentioned, if we need to perform insert, update, or delete operations, we will use <code>ExecuteSql<\/code> methods on <code>DbContext.Database<\/code> object.<\/p>\n<p>Here is how we can use the <code>ExecuteSql<\/code> method to call our <code>UpdateStudentMark<\/code> stored procedure:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int UpdateStudentMarkSql(AppDbContext context, int id, int mark)\r\n{\r\n    return context.Database.ExecuteSql($\"UpdateStudentMark @Id={id}, @Mark={mark}\");\r\n}<\/pre>\n<p>We can also use the async version:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public async static Task&lt;int&gt; UpdateStudentMarkSqlAsync(AppDbContext context, int id, int mark)\r\n{\r\n    return await context.Database.ExecuteSqlAsync($\"UpdateStudentMark @Id={id}, @Mark={mark}\");\r\n}<\/pre>\n<p>Same as <code>FromSql<\/code> and <code>FromSqlInterpolated<\/code> methods, <code>ExecuteSql<\/code> and <code>ExecuteSqlInterpolated<\/code> methods are safe against SQL injection attacks.<\/p>\n<h3>Database.ExecuteSqlInterpolated<\/h3>\n<p>Usage of the <code>ExecuteSqlInterpolated<\/code> method is the same as for <code>ExecuteSql<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int UpdateStudentMarkSqlInterpolated(AppDbContext context, int id, int mark)\r\n{\r\n    return context.Database.ExecuteSqlInterpolated($\"UpdateStudentMark @Id={id}, @Mark={mark}\");\r\n}\r\n<\/pre>\n<p>And the async version:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public async static Task&lt;int&gt; UpdateStudentMarkSqlInterpolatedAsync(AppDbContext context, int id, int mark)\r\n{\r\n    return await context.Database.ExecuteSqlInterpolatedAsync($\"UpdateStudentMark @Id={id}, @Mark={mark}\");\r\n}<\/pre>\n<h3>Database.ExecuteSqlRaw<\/h3>\n<p>And again, the same as <code>FromSqlRaw<\/code>, the <code>ExecuteSqlRaw<\/code> method needs to be used with caution for the same reasons.<\/p>\n<p>Here we see how we can use it properly with both sync:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int UpdateStudentMarkSqlRaw(AppDbContext context, int id, int mark)\r\n{\r\n    return context.Database.ExecuteSqlRaw(\"dbo.UpdateStudentMark @Id, @Mark\",\r\n        new SqlParameter(\"@Id\", id),\r\n        new SqlParameter(\"@Mark\", mark));\r\n}\r\n<\/pre>\n<p>And async version:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public async static Task UpdateStudentMarkSqlRawAsync(AppDbContext context, int id, int mark)\r\n{\r\n    return await context.Database.ExecuteSqlRawAsync(\"dbo.UpdateStudentMark @Id, @Mark\",\r\n        new SqlParameter(\"@Id\", id),\r\n        new SqlParameter(\"@Mark\", mark));\r\n}\r\n<\/pre>\n<p>Let&#8217;s see one example of calling a stored procedure with a return value. For this, a stored procedure needs to have an output parameter defined, and we need to define an output SQL parameter when we call that procedure:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int UpdateStudentMarkWithReturnValueSqlRaw(AppDbContext context, int id, int mark)\r\n{\r\n    var avgMarkParameter = new SqlParameter()\r\n    {\r\n        ParameterName = \"AvgMark\",\r\n        SqlDbType = System.Data.SqlDbType.Int,\r\n        Direction = System.Data.ParameterDirection.Output\r\n    };\r\n\r\n    context.Database.ExecuteSqlRaw(\"dbo.UpdateStudentMarkWithReturnValue @Id, @Mark, @AvgMark OUTPUT\",\r\n        new SqlParameter(\"@Id\", id),\r\n        new SqlParameter(\"@Mark\", mark),\r\n        avgMarkParameter);\r\n\r\n    return (int)avgMarkParameter.Value;\r\n}<\/pre>\n<h2>Dynamic SQL<\/h2>\n<p>Now that we know how to use all these different methods to execute SPs, we are going to show a few more features that they support, which are not so much related only to stored procedures.<\/p>\n<p>Let&#8217;s consider a situation where we need to create the query dynamically. It might seem like the interpolated string is the way to go:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int UpdateStudentMarkSqlDynamic(AppDbContext context, int id, int mark)\r\n{\r\n    var field1 = \"@Id\";\r\n    var field2 = \"@Mark\";\r\n\r\n    return context.Database.ExecuteSql($\"UpdateStudentMark {field1}={id}, {field2}={mark}\");\r\n}<\/pre>\n<p>But this is not possible because databases do not allow parameterizing parameter names or any other part of the schema, and this method would throw an <code>SqlException<\/code>.<\/p>\n<p>In this situation, we can use the raw method to create the query:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int UpdateStudentMarkSqlRawDynamic(AppDbContext context, int id, int mark)\r\n{\r\n    var field1 = \"@Id\";\r\n    var field2 = \"@Mark\";\r\n\r\n    return context.Database.ExecuteSqlRaw($\"dbo.UpdateStudentMark {field1} = @Id, {field2}=@Mark\",\r\n        new SqlParameter(\"@Id\", id),\r\n        new SqlParameter(\"@Mark\", mark));\r\n}\r\n<\/pre>\n<p>Here we still want to send input values as <code>SqlParameter<\/code> objects to prevent possible SQL injection attacks.<\/p>\n<h2>Combining With LINQ<\/h2>\n<p>It is worth pointing out it is possible to combine SQL commands with LINQ operators.\u00a0<\/p>\n<p>This is not directly applicable to the stored procedure, so we will define a method that uses a simple SELECT query in combination with LINQ:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static List? FindStudentsFromSqlAndLinq(AppDbContext context, string searchFor)\r\n{\r\n    return context?.Students?\r\n        .FromSql($\"SELECT * FROM Students\")\r\n        .Where(m =&gt; m.Name != null &amp;&amp; m.Name.IndexOf(searchFor) &gt; -1)\r\n        .OrderBy(m =&gt; m.Mark)\r\n        .ToList();\r\n}\r\n<\/pre>\n<p>We can also inspect the SQL query this method generates:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"sql\">DECLARE @__searchFor_1 nvarchar(4000) = N'100';\r\nSELECT [e].[Id], [e].[CourseId], [e].[Mark], [e].[Name]\r\n    FROM (\r\n        SELECT * FROM Students\r\n    ) AS [e]\r\nWHERE ([e].[Name] IS NOT NULL) AND \r\nCASE WHEN @__searchFor_1 = N'' THEN 0\r\nELSE CAST(CHARINDEX(@__searchFor_1, [e].[Name]) AS int) - 1 END &gt; -1\r\nORDER BY [e].[Mark]<\/pre>\n<p>We see that LINQ queries are applied on top of the initial SQL query.<\/p>\n<h2>Change Tracking<\/h2>\n<p>Queries that use the methods we just described follow the same change tracking rules as LINQ queries. EF tracks these results by default, as it tracks any other result from a LINQ query.<\/p>\n<p>Let&#8217;s define a method that demonstrates this:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static int? FindStudentsFromSqlAndUpdateMarks(AppDbContext context, string searchFor)\r\n{\r\n    var students = context?.Students?\r\n                .FromSql($\"FindStudents {searchFor}\").ToList();\r\n\r\n    if (students != null)\r\n        foreach (var student in students)\r\n            student.Mark += 1;\r\n\r\n    return context?.SaveChanges();\r\n}<\/pre>\n<p>The results we get from the stored procedure are valid entities, and we can work with them as with any other collection of entities.<\/p>\n<p>In this case, the <code>Context.SaveChanges<\/code> method will successfully save updated data to the database.<\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned how to work with stored procedures from EF Core 7.0.<\/p>\n<p>We also examined the limitations and possible security pitfalls when executing stored procedures through available EF Core methods.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we will see how to use stored procedures in Entity Framework Core 7. Although Entity Framework and LINQ provide a simple way to work with the data, stored procedures still have their place when considering data access and manipulation. Setup Let&#8217;s define our data model and stored procedures.\u00a0 We will use the [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":62194,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[22,343,1504,1505,1501,1503,1502,1500],"class_list":["post-77475","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-net-core","tag-ef-core","tag-executesql","tag-executesqlraw","tag-fromsql","tag-fromsqlinterpolated","tag-fromsqlraw","tag-stored-procedures","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Execute Stored Procedures With EF Core 7 - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Execute Stored Procedures With EF Core 7 - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-22T07:00:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-02T08:48:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ivan Matec\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ivan Matec\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\"},\"author\":{\"name\":\"Ivan Matec\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664\"},\"headline\":\"How to Execute Stored Procedures With EF Core 7\",\"datePublished\":\"2022-11-22T07:00:55+00:00\",\"dateModified\":\"2022-12-02T08:48:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\"},\"wordCount\":1022,\"commentCount\":15,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png\",\"keywords\":[\".NET CORE\",\"EF Core\",\"ExecuteSql\",\"ExecuteSqlRaw\",\"FromSQL\",\"FromSqlInterpolated\",\"FromSqlRaw\",\"Stored Procedures\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\",\"url\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\",\"name\":\"How to Execute Stored Procedures With EF Core 7 - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png\",\"datePublished\":\"2022-11-22T07:00:55+00:00\",\"dateModified\":\"2022-12-02T08:48:27+00:00\",\"description\":\"In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png\",\"width\":1100,\"height\":620,\"caption\":\"Entity Framework\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Execute Stored Procedures With EF Core 7\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664\",\"name\":\"Ivan Matec\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png\",\"caption\":\"Ivan Matec\"},\"description\":\"Ivan has over twelve years of experience developing .NET and web applications. His experience includes developing several web-based solutions for medical institutions. Other projects include developing customized CRM and ERP solutions. Besides his every-day job, he enjoys experimenting with advanced software solutions (Microsoft Azure).\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ivan-matec\/\"],\"url\":\"https:\/\/code-maze.com\/author\/tecmaivan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Execute Stored Procedures With EF Core 7 - Code Maze","description":"In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/","og_locale":"en_US","og_type":"article","og_title":"How to Execute Stored Procedures With EF Core 7 - Code Maze","og_description":"In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.","og_url":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/","og_site_name":"Code Maze","article_published_time":"2022-11-22T07:00:55+00:00","article_modified_time":"2022-12-02T08:48:27+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png","type":"image\/png"}],"author":"Ivan Matec","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Ivan Matec","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/"},"author":{"name":"Ivan Matec","@id":"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664"},"headline":"How to Execute Stored Procedures With EF Core 7","datePublished":"2022-11-22T07:00:55+00:00","dateModified":"2022-12-02T08:48:27+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/"},"wordCount":1022,"commentCount":15,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png","keywords":[".NET CORE","EF Core","ExecuteSql","ExecuteSqlRaw","FromSQL","FromSqlInterpolated","FromSqlRaw","Stored Procedures"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/","url":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/","name":"How to Execute Stored Procedures With EF Core 7 - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png","datePublished":"2022-11-22T07:00:55+00:00","dateModified":"2022-12-02T08:48:27+00:00","description":"In this article, we are going to ;earn how to execute stored procedures in Entity Framework Core 7 with different examples.","breadcrumb":{"@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/efcore-execute-stored-procedures\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-entity-framewrok.png","width":1100,"height":620,"caption":"Entity Framework"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/efcore-execute-stored-procedures\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Execute Stored Procedures With EF Core 7"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664","name":"Ivan Matec","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png","caption":"Ivan Matec"},"description":"Ivan has over twelve years of experience developing .NET and web applications. His experience includes developing several web-based solutions for medical institutions. Other projects include developing customized CRM and ERP solutions. Besides his every-day job, he enjoys experimenting with advanced software solutions (Microsoft Azure).","sameAs":["https:\/\/www.linkedin.com\/in\/ivan-matec\/"],"url":"https:\/\/code-maze.com\/author\/tecmaivan\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/77475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=77475"}],"version-history":[{"count":5,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/77475\/revisions"}],"predecessor-version":[{"id":77863,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/77475\/revisions\/77863"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62194"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=77475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=77475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=77475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}