如何使用具有单个输入参数的EntityFramework在webApi中执行存储过程

时间:2015-11-04 12:00:29

标签: c# sql entity-framework stored-procedures asp.net-web-api

我用一个输入参数制作了一个Sp。我试图将它与我的web服务绑定。但我不知道如何在我的服务中调用它以及如何传递输入参数。这是我的sp

CREATE PROCEDURE StudentDetailss

@routeId varchar

AS

SELECT  dbo.Users.FirstName, dbo.Users.LastName, dbo.PSS_Grades.GradeNo, dbo.PSS_Divisions.Name AS DivisionName, dbo.PSS_Stops.StopName, 
        dbo.PSS_ContactDeatils.Mobile, dbo.PSS_Routes.RouteId, dbo.PSS_Routes.RouteName, dbo.PSS_Students.StudentID, dbo.PSS_RelationShips.RelationshipID, 
        MIN(dbo.PSS_RelationShips.RelationTypeID) AS RelationTypeID, Users_1.FirstName AS ParentFname, Users_1.LastName AS ParentLname

FROM    dbo.Users AS Users_1 INNER JOIN
        dbo.PSS_ContactDeatils ON Users_1.UserID = dbo.PSS_ContactDeatils.UserID INNER JOIN
        dbo.PSS_RelationShips INNER JOIN
        dbo.Users ON dbo.PSS_RelationShips.StudentUserID = dbo.Users.UserID INNER JOIN
        dbo.PSS_Routes INNER JOIN
        dbo.PSS_Students INNER JOIN
        dbo.PSS_Stops ON dbo.PSS_Students.StopId = dbo.PSS_Stops.StopId ON dbo.PSS_Routes.RouteId = dbo.PSS_Stops.RouteId ON dbo.Users.UserID = dbo.PSS_Students.StudentID AND 
        dbo.PSS_RelationShips.StudentUserID = dbo.PSS_Students.StudentID ON Users_1.UserID = dbo.PSS_RelationShips.RelatedUserID INNER JOIN
        dbo.PSS_Grades INNER JOIN
        dbo.PSS_StudentDivisions ON dbo.PSS_Grades.GradeID = dbo.PSS_StudentDivisions.GradeID INNER JOIN
        dbo.PSS_Divisions ON dbo.PSS_Grades.GradeID = dbo.PSS_Divisions.GradeID AND dbo.PSS_StudentDivisions.DivisionID = dbo.PSS_Divisions.DivisionID ON 
        dbo.PSS_Students.StudentID = dbo.PSS_StudentDivisions.StudentUserID

WHERE   PSS_Routes.RouteId=@routeId

GROUP BY    dbo.Users.FirstName, dbo.Users.LastName, dbo.PSS_Grades.GradeNo, dbo.PSS_Divisions.Name, dbo.PSS_Stops.StopName, dbo.PSS_ContactDeatils.Mobile, 
            dbo.PSS_Routes.RouteId, dbo.PSS_Routes.RouteName, dbo.PSS_Students.StudentID, dbo.PSS_RelationShips.RelationshipID, Users_1.FirstName, Users_1.LastName

HAVING  (MIN(dbo.PSS_RelationShips.RelationTypeID) <> 3)

我已完成功能导入并从此SP中创建了一个复杂的Type。现在我想把它叫做我的Api。怎么可能

1 个答案:

答案 0 :(得分:2)

如果您已导入您的函数,则可通过上下文对象访问它,如下所示:

public IEnumerable<StudentDetailss_result> GetStudentDetails(string routeId)
{
    using(var ctx = new YourContextHere())
    {
        var result = ctx.StudentDetailss(routeId);
        return result.ToList();
    }
}