在Entity框架中使用CTE进行分页
我正在尝试在 sql server 2008 中使用 CTE 并在实体框架中使用它。
以下是 SP:
CREATE PROCEDURE GetReportingCategories
-- Add the parameters for the stored procedure here
@StartRow INT,
@EndRow INT,
@SortDirection VARCHAR(50),
@SortExpression VARCHAR(50),
@TotalRecord int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
;WITH CTE (CategoryTitle, CreatedDate, UpdatedDate, [Status], RowNumber) AS
(
SELECT CategoryTitle,
CreatedDate,
UpdatedDate,
[Status],
ROW_NUMBER() OVER(ORDER BY
case when @SortExpression='CategoryTitle' and @SortDirection='DESCENDING' then CategoryTitle end DESC,
case when @SortExpression='CategoryTitle' and @SortDirection='ASCENDING' then CategoryTitle end ASC) AS RowNumber
FROM ReportingCategory
)
SELECT * INTO #TtCTE FROM CTE;
SELECT @TotalRecord = ISNULL(SUM(1), 0) FROM #TtCTE;
SELECT *
FROM #TtCTE
WHERE RowNumber >= @StartRow AND RowNumber <= @EndRow;
END
GO
SP 执行完美。但是,问题是,在 VS2010 中从数据库更新模型后,当我尝试“添加函数导入”时,我没有获得列信息。所以,无法食用。
请指导如何使用实体框架和存储过程实现不带 CTE 的自定义页面。 谢谢。
I am trying to use CTE in sql server 2008 and consume it in Entity framework.
Following is the SP:
CREATE PROCEDURE GetReportingCategories
-- Add the parameters for the stored procedure here
@StartRow INT,
@EndRow INT,
@SortDirection VARCHAR(50),
@SortExpression VARCHAR(50),
@TotalRecord int OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
;WITH CTE (CategoryTitle, CreatedDate, UpdatedDate, [Status], RowNumber) AS
(
SELECT CategoryTitle,
CreatedDate,
UpdatedDate,
[Status],
ROW_NUMBER() OVER(ORDER BY
case when @SortExpression='CategoryTitle' and @SortDirection='DESCENDING' then CategoryTitle end DESC,
case when @SortExpression='CategoryTitle' and @SortDirection='ASCENDING' then CategoryTitle end ASC) AS RowNumber
FROM ReportingCategory
)
SELECT * INTO #TtCTE FROM CTE;
SELECT @TotalRecord = ISNULL(SUM(1), 0) FROM #TtCTE;
SELECT *
FROM #TtCTE
WHERE RowNumber >= @StartRow AND RowNumber <= @EndRow;
END
GO
SP executes perfectly. But, problem is that after updating model from database in VS2010, when i try to "Add function import", than i don't get column information. So, unable to consume it.
Please guide how to implement custom page w/o CTE using Entity Framework and Stored Procedure.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用 LINQ 查询实体吗?如果可以使用“Skip”和“Take”操作可能会更容易修复。
Are you unable to use LINQ to query the entities? If you can using the Skip and Take operations may be an easier fix.
最后,我成功了。在数据库中设置以下内容,然后使用 CTE 或类似的东西:
仅供参考:我没有时间进一步研究此语句,但我已经尝试过并且它有效。
Finally, i got it working. SET the following at the database and than use CTE or similar things:
FYI: I didn't get time to investigate more about this statement, but i have tried it and it works.
我相信问题在于您正在使用内联临时表 - 更重要的是,使用 SELECT * INTO 创建它 - 因此实体框架无法弄清楚您的输出列是什么存储过程将是。
像这样的事情怎么办?
...那么您不需要临时表(或第二个查询),这应该允许实体框架查看输出列。
I believe the problem is that you are using an inline temporary table—and even more to the point, creating it using
SELECT * INTO
—and Entity Framework is therefore unable to figure out what the output columns of your stored procedure will be.What about something like this?
...then you don't need the temporary table (or a second query), which should allow Entity Framework to see the output columns.