Friday, June 12, 2015

Entity Framework and SQL Storeprocedure

I'm using Entity Framework since 2012, but no idea how to execute SQL Storeprocedures from DBContext. While developing one portal, I have a situation to call SQL Storeprocedure for performance purpose. Google and google around, and I found out a solution to execute storeprocedure in DBContext. Here is the example of DBContext example to run Storeprocedure:

var ResultSet = DBContext.Database.SqlQuery("exec [Storeprocedure name] @[parameter]", [parameter value]).ToList<[Model]>();

Here is the implementation of the above example:
var Customers = ctx.Database.SqlQuery("exec getAllCustomers").ToList();

No comments: