简体   繁体   中英

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

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

Non of the existing solutions in SO worked for me. Only the following worked for me:

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM