2012-01-13 68 views
0

我通过实体框架Code-First中的存储过程实现实体。存储过程接受一个int参数并输出记录选择和多个输出参数。无论出于何种原因,在执行SQL Management Studio中的Proc时导致预期行为时,我将返回空值作为输出参数;所有输出参数都有值。我的实体被物化,所以至少这是工作......SqlParameter在实体框架中返回null SqlQuery

EF代码

 SqlParameter DocsWithHits = new SqlParameter() 
     { 
      ParameterName = "DocumentsWithHits", 
      Direction = System.Data.ParameterDirection.Output, 
      Value = null, 
      SqlDbType = System.Data.SqlDbType.Int 
     }; 

     SqlParameter TotalGB = new SqlParameter() 
     { 
      ParameterName = "TotalGb", 
      Direction = System.Data.ParameterDirection.Output, 
      Value = null, 
      SqlDbType = System.Data.SqlDbType.Float 
     }; 

     ObservableCollection<SearchResult> ResultCollection; 
     ResultCollection = new ObservableCollection<SearchResult>(db.Database.SqlQuery<SearchResult>(
      "exec ListSearchResultsWithTotals @SearchAnalysisID, @DocumentsWithHits, @RelatedDocuments, @TotalDocuments, @TotalGb, @DocumentsWithHitsNotExported, @RelatedDocumentsNotExported, @TotalDocumentsNotExported, @TotalGbNotExported", 
      new SqlParameter 
      { 
       ParameterName = "SearchAnalysisID", 
       Value = this.SearchAnalysisId 
      }, 
      DocsWithHits, 
      TotalGB)); 

SQL事件探查器

所产生从SqlQuery类方法是下面的SQL。我在Profiler中捕获它。当执行时,我得到记录和空输出参数。

declare @p4 int 
set @p4=NULL 
declare @p5 float 
exec sp_executesql N'exec ListSearchResultsWithTotals @SearchAnalysisID, @DocumentsWithHits, @TotalGb',N'@SearchAnalysisID int,@DocumentsWithHits int output,@TotalGb float output',@SearchAnalysisID=170,@[email protected] output,@[email protected] output 
select @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11 

我使用的SqlParameter错了还是什么?

回答

1

您必须在SQL中使用OUT关键字传递给SqlQuery(示例here)。在迭代或关闭查询的主要结果集后,请确保您正在读取这些参数(它应该由ObservableCollection构造函数完成)。