Execute Stored Procedure In Entity Framework

In EF (Entity Framework), there are mainly two ways to execute the stored procedure.

Execute Command

Normally, all developers do it this way.
  1. var affectedDatas = context.Database.ExecuteSqlCommand("store_procedure_name @field1, @field2, @field3, ...",  
  2.    new SqlParameter("@field1""value1"),  
  3.    new SqlParameter("@field2""value2"),  
  4.    new SqlParameter("@field3""value3"),  
  5. ...);   
In the above scenario, if more parameters are added in the stored procedure, then we have to add that newly created parameter in this statement also. If we forget to add that, it will throw an error. 

To overcome this type of mistake, we can create an Extension method by which we can simplify this operation.
  1. public class SQLQueryClass {  
  2.     public string Command {  
  3.         get;  
  4.         set;  
  5.     }  
  6.     public SqlParameter[] Parameters {  
  7.         get;  
  8.         set;  
  9.     }  
  10. }  
  11. public static SQLQueryClass CreateCommandAndParameters(this object obj) {  
  12.     var props = obj.GetType().GetProperties();  
  13.     var result = new SQLQueryClass();  
  14.     var lstSqlParameters = new List();  
  15.     var lstName = new List();  
  16.     for (int i = 0; i < props.Length; i++) {  
  17.         var propertyName = $ "@{ props[i].Name}";  
  18.         var value = props[i].GetValue(obj, null);  
  19.         lstName.Add(propertyName);  
  20.         var sqlParameter = new SqlParameter(propertyName, value ? ? DBNull.Value);  
  21.         lstSqlParameters.Add(sqlParameter);  
  22.     }  
  23.     result.Command = string.Join(", ", lstName);  
  24.     result.Parameters = lstSqlParameters.ToArray();  
  25.     return result;  
  26. }  
This is how to use the above created method in your project.
  1. var parameters = new  
  2. {  
  3.    field1 = value1,  
  4.    field2 = value2,  
  5.    field3 = value3,  
  6. ...  
  7. };  
  8. var result = parameters.CreateCommandAndParameters();   
Then, it will return two things in that response.
  1. It will return the field name comma separated.
  2. It will return the SQL parameters.
And finally, execute the command.
  1. var command = $"store_procedure_name {result.Command}";  
  2. var affectedDatas = context.Database.ExecuteSqlCommand(command, result.Parameters);   
SQLQuery 

Normally, all developers do it this way.
  1. var affectedDatas = context.Database.SqlQuery("store_procedure_name @field1, @field2, @field3, ...",  
  2.    new SqlParameter("@field1""value1"),  
  3.    new SqlParameter("@field2""value2"),  
  4.    new SqlParameter("@field3""value3"),  
  5. ...);   
In the above scenario, if one more parameter is added in the stored procedure, then we have to add that newly created parameter in this statement also. If we fail, then it will throw an error. 

To overcome this type of mistake, we can create an Extension method by which we can simplify this operation.
  1. var result = parameters.CreateCommandAndParameters();  
  2. var command = $"store_procedure_name {result.Command}";  
  3. var affectedDatas = context.Database.SqlQuery(command, result.Parameters);