Using Stored Procedure to get records from an SQL View in EF 6

时间:2016-04-15 15:17:36

标签: c# sql-server entity-framework stored-procedures

I have a stored procedure (SP) in the database to retrieve some records from a view. When I test the SP, it returns the valid set of results. However, with the same parameter values are provided, I cannot get any result (Count=0) set in my code using EF. Here is my code:

SqlParameter dwid = new SqlParameter("DiscussionWallId", wallId);
SqlParameter pagesize = new SqlParameter("PageSize", 10);
SqlParameter pindex = new SqlParameter("PageIndex", pageIndex);

object[] parameters = new object[] { dwid, pagesize, pindex };

var entryviews = db.Database.SqlQuery<ViewEntryRecord>("EXEC FetchMainEntries  @DiscussionWallId, @PageSize, @PageIndex", parameters).ToList();

I found this thread but SET FMTONLY OFF did not work for me. Any Help?

Here is the SP:

USE [CBV]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[FetchMainEntries]    
    @PageIndex INT,
    @PageSize INT,
    @DiscussionWallId INT
AS
IF 1=0 BEGIN
    SET FMTONLY OFF
END
SELECT .... //QUERY part starts here

1 个答案:

答案 0 :(得分:0)

SO中现有的解决方案都不适用于我。只有下面的内容对我有用:

var entryviews = db.Database.SqlQuery<ViewEntryRecord>("DECLARE @return_value int;
   EXEC @return_value = [dbo].[FetchMainEntries] @PageIndex = {0}, 
   @PageSize = {1}, @DiscussionWallId = {2}, @ChildSize={3};
   SELECT  'Return Value' = @return_value;", 
   pageIndex, 10, wallId, 2).ToList();