Can anyone explain Executing Stored Procedure from EF also that procedure should accept data in array list
Loading
Can anyone explain Executing Stored Procedure from EF also that procedure should accept data in array list
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Dec 13, 2024, 12:32 AM EST
Hello Kiran,
You can create as below,
using System.Data; using Microsoft.Data.SqlClient; var dictionary = new Dictionary { { 1, ("Name1", "Value1") }, { 2, ("Name2", "Value2") }, { 3, ("Name3", "Value3") } }; // Convert dictionary to DataTable var dataTable = new DataTable(); dataTable.Columns.Add("Id", typeof(int)); dataTable.Columns.Add("KeyName", typeof(string)); dataTable.Columns.Add("Value", typeof(string)); foreach (var kvp in dictionary) { dataTable.Rows.Add(kvp.Key, kvp.Value.KeyName, kvp.Value.Value); }Call Store procedure from EF
var parameter = new SqlParameter("@KeyValueTable", dataTable) { SqlDbType = SqlDbType.Structured, TypeName = "dbo.KeyValueTableType" }; using (var context = new YourDbContext()) { context.Database.ExecuteSqlRaw("EXEC ProcessDictionary @KeyValueTable", parameter); }Create your store procedure,
CREATE TYPE KeyValueTableType AS TABLE ( Id INT, KeyName NVARCHAR(MAX), Value NVARCHAR(MAX) ); CREATE PROCEDURE ProcessDictionary @KeyValueTable KeyValueTableType READONLY AS BEGIN SET NOCOUNT ON; -- You can create your select statement using where clause here SELECT * FROM @KeyValueTable; END;Rajeesh MenothRank #118 · 18.4K reputationPosted Dec 12, 2024, 11:38 PM EST
Hi Kiran,
To execute a stored procedure from Entity Framework that accepts a list (array) as a parameter, you can use the following article approach with the help of Table-Valued Parameters (TVP).
Try this steps.
var myList = new List { 1, 2, 3, 4, 5 }; var result = dbContext.ExecuteMyStoredProcedure(myList);Kiran KumarRank #1,268 · 1.4K reputationPosted Dec 12, 2024, 11:47 PM EST
We need to configure the parameter data type as we do in SQLCommand , here, will it identify with not mentioning parameter type just with procedure name from ExecuteMyStoredProcedure function
Also, my requirement is sending the details in dictionary
I edit multiple rows in grid and click apply , it will send to procedure and update the data by Id
Few times it may requires multiple data points for that same Id ex :