Showing posts with label Entity Frame Work. Show all posts
Showing posts with label Entity Frame Work. Show all posts

Call Store procedure in Entity Frame Work

This post is about how to use stored procedure in Entity Framework Code First



1

2

3

ALTER PROCEDURE dbo.GetUsersByRoleId(@RoleId int)

AS

SELECT * FROM Users WHERE RoleId = @RoleId


And here is code snippet which will help to execute this Stored procedure.

1
2
3
4
5
6
public IQueryable<User> GetUsersByRoleId(int roleId)
{
    var parameter = new SqlParameter("RoleId", SqlDbType.Int) { Value = roleId };
    return _timeSheetDataContext.Users.SqlQuery("GetUsersByRoleId @RoleId",
        new[] { parameter }).AsQueryable();
}