在Entity框架中使用CTE进行分页

发布于 2024-11-18 19:26:53 字数 1304 浏览 3 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烟燃烟灭 2024-11-25 19:26:53

您无法使用 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.

眸中客 2024-11-25 19:26:53

最后,我成功了。在数据库中设置以下内容,然后使用 CTE 或类似的东西:

SET FMTONLY OFF

仅供参考:我没有时间进一步研究此语句,但我已经尝试过并且它有效。

Finally, i got it working. SET the following at the database and than use CTE or similar things:

SET FMTONLY OFF

FYI: I didn't get time to investigate more about this statement, but i have tried it and it works.

天邊彩虹 2024-11-25 19:26:53

我相信问题在于您正在使用内联临时表 - 更重要的是,使用 SELECT * INTO 创建它 - 因此实体框架无法弄清楚您的输出列是什么存储过程将是。

像这样的事情怎么办?

CREATE PROCEDURE GetReportingCategories
    -- Add the parameters for the stored procedure here    
    @StartRow INT,
    @EndRow INT,
    @SortDirection VARCHAR(50),
    @SortExpression VARCHAR(50)
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 
        (SELECT COUNT(*) FROM CTE) AS TotalRecords,
        CategoryTitle,             
        CreatedDate,
        UpdatedDate,
        [Status]
    FROM CTE
    WHERE RowNumber >= @StartRow AND RowNumber <= @EndRow
END

...那么您不需要临时表(或第二个查询),这应该允许实体框架查看输出列。

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?

CREATE PROCEDURE GetReportingCategories
    -- Add the parameters for the stored procedure here    
    @StartRow INT,
    @EndRow INT,
    @SortDirection VARCHAR(50),
    @SortExpression VARCHAR(50)
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 
        (SELECT COUNT(*) FROM CTE) AS TotalRecords,
        CategoryTitle,             
        CreatedDate,
        UpdatedDate,
        [Status]
    FROM CTE
    WHERE RowNumber >= @StartRow AND RowNumber <= @EndRow
END

...then you don't need the temporary table (or a second query), which should allow Entity Framework to see the output columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文