The following is the description of executing stored procedure with Entity Framework.
It describes two separate methods for reading data and inserting/updating data through stored procedure.
The following is the code.
1. Fetching data
3. Helper method
5. Implementing Read
6. Implementing Insert/Update
It describes two separate methods for reading data and inserting/updating data through stored procedure.
The following is the code.
1. Fetching data
static List2. Insert/UpdateExecuteReadStoredProcedure (string storedProcName, Dictionary parameters)
{
using (var entityContext = new ImplementedDbContext())
{
var storedProcedureParams = GetStoredProcedureParameters(storedProcName, parameters);
return entityContext.Database.SqlQuery(storedProcedureParams.StoredProcedureName,
storedProcedureParams.Parameters.ToArray()).ToList();
}
}
static int ExecuteInsertUpdateStoredProcedure(string storedProcName, Dictionaryparameters)
{
using (var entityContext = new ImplementedDbContext())
{
var storedProcedureParams = GetStoredProcedureParameters(storedProcName, parameters);
return entityContext.Database.ExecuteSqlCommand(storedProcedureParams.StoredProcedureName,storedProcedureParams.Parameters.ToArray());
}
}
3. Helper method
private static StoredProcedureParams GetStoredProcedureParameters(string storedProcedureName,Dictionary4. Object Parameterparameters) {
StringBuilder query = new StringBuilder();
var listOfParameters = new List
class StoredProcedureParams {
public string StoredProcedureName { get; set; }
public List
5. Implementing Read
var dictParameters = new Dictionary5.1 Response Object();
dictParameters.Add("@parameter1",value of parameter 1);
dictParameters.Add("@parameter2",value of parameter 2);
var readResponse = ExecuteReadStoredProcedure("Your-Read-StoredProcedureName", dictParameters);
public class MyResponse
{
public int Id { get; set; }
public string Name { get; set; }
}
6. Implementing Insert/Update
var dictParametersCreateEmailTemp = new Dictionary();
dictParametersCreateEmailTemp.Add("@parameter1","value");
dictParametersCreateEmailTemp.Add("@parameter2", "value");
dictParametersCreateEmailTemp.Add("@parameter3", "value");
dictParametersCreateEmailTemp.Add("@parameter4", "value");
var insertResponse = ExecuteInsertUpdateStoredProcedure("Your-Insert-StoredProcedureName", dictParametersCreateEmailTemp);