November 08, 2013 by Anuraj Estimated read time : 3 mins
.NetASP.Net MVCEF Code FirstEntity FrameworkSQL Server
Recently I worked on a project, which I started as code first and then I forced to switch to Database first. This post is about executing procedures from EF code first. Here is my class structure and procedures.
And here is my stored procedures
And you can execute using DbContext.Database class. The DbContext.Database.ExecuteSqlCommand() method helps to executes the given DDL/DML command against the database. And it will return the number of rows affected.
Or you can use without creating the SqlParameters.
The DbContext.Database.SqlQuery method helps to return elements of the given generic type. The type can be any type that has properties that match the names of the columns returned from the query, or can be a simple primitive type.
This method will return an DbRawSqlQuery, which you can enumerate using For / ForEach loop. For executing procedure with output parameter.