在EF中使用定义对象模型的方式调用一个存储过程,这个存储过程返回的是一组包含两列的值。(ProjectName,Count)
下面是存储过程:
CREATE procedure [dbo].[Pro_Project_By_UserID]
@userID nvarchar(36)
AS
SELECT Project AS ProjectTypeName,
Count(BA.ProjectId) AS ProjectCount
FROM dbo.B_AllProjectInfo BA
WHERE IsComplex = 1
AND UserId = @userID
GROUP BY Project
在DB中定义了这个SP后,在C#中的*.edmx中,选择‘从数据库更新模型’,更新方式和更新表一样。这样在C#中就会生成
Pro_Project_By_UserID_Result
的类,还有一个函数
public virtual ObjectResult<Pro_Project_By_UserID_Result> Pro_Project_By_UserID(string userID)
{
var userIDParameter = userID != null ?
new ObjectParameter("userID", userID) :
new ObjectParameter("userID", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<Pro_Project_By_UserID_Result>("Pro_Project_By_UserID_UserID", userIDParameter);
}
在实际执行时,代码如下:
var curProjects = dataContext.Pro_Project_By_UserID(myUserID).ToList();
要加上ToList(),否则返回的值不能用