2016-04-27 14 views
0

以下の出力パラメータを持つストアドプロシージャがあります。これは、SQLサーバーで完璧に動作しているSQL Serverの出力パラメータがEFでNULL値を示していますC#

ALTER proc [dbo].[CRS_GetNewMessageCount] 
@CaseId int, 
@Type varchar(50), 
@Location varchar(50), 
@Count int out 
as 

begin 
if @location='admin' 
    begin 
    if @type='consumer' 
    select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsConsumerType=1 and fdIsReadatAdmin=0) 
    else 
    select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsMemberType=1 and fdIsReadatAdmin=0) 
    end 
else 
begin 
    if @type='consumer' 
    select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsConsumerType=1 and fdIsReadatFront=0) 
else 
select @Count=(Select count(fdId) from tb_CRS_Messages where [email protected] and fdIsMemberType=1 and fdIsReadatFront=0) 
END 
SELECT @Count 
END 

が出力以下を参照してください。

私はEntity Frameworkを通じてこのstored procedureを呼び出しています:

using (DbContext db = new DbContext()) 
      { 
       try 
       { 
        var NewMessage = new ObjectParameter("Count", typeof(int)); 
        int returnValue = 0; 
        db.CRS_GetNewMessageCount(CaseId, type, location, NewMessage); 
        int.TryParse(NewMessage.Value.ToString(), out returnValue); 
        return returnValue; 
       } 
       catch { return 0; } 
      } 

それは、出力パラメータにnull値を与えます。

助けてください。

+0

を助け、それがSQL管理スタジオで働きますか? – jamiedanq

+0

'SELECT @ Count'の使用は冗長です。 'null'を入力するので、' out'パラメータの値を取得しません。 –

+0

これは、ストアドプロシージャの横のFirstOrDefault()を使用して解決しました。 ご協力いただきありがとうございます。 –

答えて

1

"typeof(int)"の代わりに "typeof(Int32)"を使用しようとしましたか?また、NewMessageをvarとしてではなくObjectParameterとして試してみてください。おそらく違いがあります。

ObjectParameter NewMessage = new ObjectParameter("NewMessage ", typeof(Int32)); 

あなたが返す意味し、パラメーターを指定せずに、あなたのストアドプロシージャを実行しようとしました@カウントしない変数の値と関係なく、あなたが入力したパラメータを何?このように、入力パラメータが間違っているかどうか(C#によって引き渡されるかどうか)を識別できます。