0

I want to call a stored procedure that does not belong to my .EDMX, but the procedure exists in the database.

Could I call it from my code even if the procedure is not in my .EDMX?

If it's possible could you provide me with some sample code?

Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Luka
  • 67
  • 7
  • Procedural code is **highly vendor-specific** - so please add a tag to specify whether you're using `mysql`, `postgresql`, `sql-server`, `oracle` or `db2` - or something else entirely. – marc_s May 23 '19 at 19:22
  • 1
    which of these did you try? https://stackoverflow.com/questions/20901419/how-to-call-stored-procedure-in-entity-framework-6-code-first – Caius Jard May 23 '19 at 19:26
  • Oracle database – Luka May 23 '19 at 19:58

1 Answers1

1

You can execute the stored procedure directly like this: call stored procedure GetEmployeById with parameter @Id

using (var ctx = new DBEntities())
{
    var idParam = new SqlParameter { ParameterName = "Id",Value = 1};

    //Get employee by id
    var employeeList = ctx.Database.SqlQuery<Employee>("exec GetEmployeById @Id ", idParam).ToList<Employee>();

    foreach (employee emp in employeeList)
       Console.WriteLine("Employee Name: {0}",emp.Name);
  }       

For more documentation check this link

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ahmed Yousif
  • 2,298
  • 1
  • 11
  • 17