存储过程返回int值而不是Entity Framework中的结果集

时间:2015-12-10 07:34:25

标签: entity-framework sql-server-2008

我有这个存储过程

create procedure [dbo].[sp_GetAllLesiureActivitiesNew]
    (@ActivityLeaderID int = null)
as 
begin
    declare @TempLeisureActivites TABLE
        ([ActivityPlan_ID] [int]  NULL,
         [ActivityRecurrence_ID] [int] NULL,
         [ActivityName] [nvarchar](50) NULL,
         [Activity] [nvarchar](max) NULL,   
         [IsResponse] [bit] NULL,
         [IsLOI] [bit] NULL,
         [clientcount] [int] NULL,  
         [Location] [nvarchar](max) NULL,
         [StartDateTime] [datetime] NULL,
         [EndDatetime] [datetime] NULL)

    insert into @TempLeisureActivites
        select distinct 
            ap.ActivityPlan_ID, ap.ActivityRecurrence_ID, 
            ap.ActivityName, orgla.Activity, orgla.IsResponse,
            orgla.IsLOI,
            (select count(distinct cc.ID ) from Client cc join Activity_Clients acc on cc.ID=acc.Client_ID join ActivityPlan app on acc.ActivityRecurrence_ID=app.ActivityRecurrence_ID where app.ActivityRecurrence_ID=ap.ActivityRecurrence_ID  and cc.Status=1) as clientcount,l.Location,ap.StartDateTime,ap.EndDatetime 
from ActivityPlan ap
left outer join Location l on ap.ActivityLocationID =l.ID left join Activity_Clients ac on ap.ActivityRecurrence_ID=ac.ActivityRecurrence_ID 
left outer join Client c on ac.Client_ID=c.ID
left outer join LeisureActivity orgla on ap.ActivityType=orgla.ID 
where ap.ActivityLeaderID in(0,@ActivityLeaderID) and c.[Status]=1

    if(@ActivityLeaderID is not null and @ActivityLeaderID>0)
    begin


declare @Activities nvarchar(max)
declare @Locations nvarchar(max)
declare @CISelection nvarchar(50)
declare @IsAllLocs bit

declare @TempRecurID int
declare @TempAPID int

--BEGIN TRANSACTION T1
--BEGIN TRY

 declare @RecurIDsCursor CURSOR
declare @RecurIDsRowsCount int

 --print 'Before cursor logic starts'
set @RecurIDsCursor=CURSOR STATIC FOR
select ActivityPlan_ID,ActivityRecurrence_ID from @TempLeisureActivites

OPEN @RecurIDsCursor
set @RecurIDsRowsCount=(SELECT @@CURSOR_ROWS)

--print 'cursor rows count:'+cast(@RecurIDsRowsCount as nvarchar)

FETCH NEXT
FROM @RecurIDsCursor INTO @TempAPID,@TempRecurID

WHILE @RecurIDsRowsCount>0
BEGIN

--select @Activities=NULL,@Locations=NULL
--print 'looping started...'
select @Activities='',@Locations=''

select @CISelection=NULL,@IsAllLocs=0

--print 'Activity Plan ID'+cast(@TempAPID as nvarchar)+',Recur ID:'+ cast(@TempRecurID as nvarchar)

select @CISelection=[CommonInterestsSelection] from [dbo].[ActivityPlan] where ActivityPlan_ID=@TempAPID and [ActivityRecurrence_ID]=@TempRecurID


--print 'CI Selection:'+@CISelection

if(@CISelection='Specific')
begin
select @Activities+=(
case when la.Activity is not null then 
 ISNULL(la.Activity,'')+',' end) from [dbo].[ActivityPlan_Filters] apf
left outer join [dbo].[LeisureActivity] la on la.ID=apf.FilterID 
where [ActivityRecurrence_ID]=@TempRecurID and apf.FilterType='Common_Interests'

if(LEN(@Activities)>0)
begin
select @Activities=LEFT(@Activities, LEN(@Activities) - 1)
end
end
else if(@CISelection='Top')
begin
select @Activities=[CommonInterestValue] from [dbo].[ActivityPlan] where ActivityPlan_ID=@TempAPID and [ActivityRecurrence_ID]=@TempRecurID
end
else if(@CISelection='NA')
begin
select @Activities='ALL'
end

--print 'Activities:'+@Activities
select @IsAllLocs=[IsAllLocations] from [dbo].[ActivityPlan] where ActivityPlan_ID=@TempAPID and [ActivityRecurrence_ID]=@TempRecurID
if(@IsAllLocs=1)
begin
select @Locations='ALL'
end
else if(@IsAllLocs=0)
begin
select @Locations+=(
case when loc.Location is not null then 
 ISNULL(loc.Location,'')+',' end) from [dbo].[ActivityPlan_Filters] apf
left outer join [dbo].[Location] loc on loc.ID=apf.FilterID 
where [ActivityRecurrence_ID]=@TempRecurID and apf.FilterType='Locations'

if(LEN(@Locations)>0)
begin
select @Locations=LEFT(@Locations, LEN(@Locations) - 1)
end
end

--print 'Locations:'+@Locations

--print 'before updation'
update @TempLeisureActivites
set Activity=@Activities,Location=@Locations
where ActivityPlan_ID=@TempAPID and ActivityRecurrence_ID=@TempRecurID

--print 'after updation'

FETCH NEXT
FROM @RecurIDsCursor INTO @TempAPID,@TempRecurID

SET @RecurIDsRowsCount=@RecurIDsRowsCount-1
END
CLOSE @RecurIDsCursor
DEALLOCATE @RecurIDsCursor


end
select * from @TempLeisureActivites
end

它在SQL Server Management Studio中执行时返回Result set,但在使用Entity Framework的Asp.net MVC中,它返回一个整数而不是结果集,如下所示。

  public virtual int sp_GetAllLesiureActivitiesNew(Nullable<int> activityLeaderID)
        {
            var activityLeaderIDParameter = activityLeaderID.HasValue ?
                new ObjectParameter("ActivityLeaderID", activityLeaderID) :
                new ObjectParameter("ActivityLeaderID", typeof(int));

            return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("sp_GetAllLesiureActivitiesNew", activityLeaderIDParameter);
        }

我发现了一篇文章,但它对我没有帮助(Stored procedure returns int instead of result set)。 我怎么能解决我的问题?

1 个答案:

答案 0 :(得分:1)

ExecuteFunction()方法返回受整数影响的行数。

如果您希望它返回ObjectResult<YourEntityType>。 将其更改为:

return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<YourEntityType>("sp_GetAllLesiureActivitiesNew", activityLeaderIDParameter);

但我认为现在更好的方法是:

Dbcontext.Database.SqlQuery<YourEntityType>("storedProcedureName",params);

Here是一篇展示示例的文章。