Call store procedure which does not return anything using Entity Framework

by sunil ravulapalli /10. May 2013 18:29 /entityframework /Comments (0)

This is the technique I am using currently, doesn't feel correct but works.

Main EF function

public int ExecWithStoreProcedure(string query, params object[] parameters)
{ 
   var _context = new CompanyDbContext();
   var ints = _context.Database.SqlQuery(query, parameters).ToList(); 
   return ints[0]; 
}

Caller function

public long InsertEmployee(string empName, decimal empSalary) 
{ 
   var lineIdParam = new SqlParameter("lineid", SqlDbType.BigInt) { Direction = ParameterDirection.Output, Size = 8 }; 
     var effectiveDateParam =  new SqlParameter("effectiveDate", SqlDbType.DateTime)  { Direction = ParameterDirection.Output, Size = 8 }; 

   ExecWithStoreProcedure("spInsertStuff @name, @salary, @lineid OUT, @effectiveDate OUT", 
                                      new SqlParameter("name", SqlDbType.Varchar)   { Value = empName }, 
                                      new SqlParameter("salary", SqlDbType.Decimal) { Value = empSalary}, 
                                      lineIdParam, 
                                      effectiveDateParam); 
   return lineIdParam.Value; 
}

 

blog comments powered by Disqus