2014-12-03 36 views
0

我剛開始使用Azure'移動服務',我使用C#作爲後端。我試圖返回Guid回到客戶端,但我收到錯誤:「Cannot convert source type int to target type Guid」。C#後端移動服務:return Guid

這裏是我的代碼:

型號:

public class MobileRegistration : EntityData 
    { 
     public Guid MobileRegistrationId { get; set; } 

    } 

控制器:

public async Task<Guid> GetMobileRegistrationId() 
      { 
       using (var context = new CustomeeMobileServiceContext()) 
       {      
        var db = context.Database; 

        var model = new MobileRegistration();  

        // The error is here (I need to get Guid from the SP): 
        //Error: Cannot convert source type int to target type Guid   
        model.MobileRegistrationId = await db.ExecuteSqlCommandAsync("EXEC dbo.MobileRegistration.GetMobileRegistrationId"); 

        return model.MobileRegistrationId; 
       } 
      } 

存儲過程:

Create PROCEDURE GetMobileRegistrationId  
AS 

DECLARE @MobileRegistrationID uniqueidentifier 
    set @MobileRegistrationID = NEWID() 

INSERT  
    INTO MobileRegistration (MobileRegistrationID) 
VALUES  (@MobileRegistrationID) 

select @MobileRegistrationID as MobileRegistrationID 
+0

而且你甚至不打擾告訴我們哪裏......你得到的錯誤。驚人。有一些水晶球?不要傾倒我們很多代碼,然後認爲這是我們的工作來完成你的工作。 – TomTom 2014-12-03 09:01:09

+0

不要生氣......我會添加它。 – Eyal 2014-12-03 09:02:35

+0

寧可隔離錯誤並丟棄不需要的其餘部分。根據常見問題,您必須提供必要的最小代碼來重現問題。 – TomTom 2014-12-03 09:03:31

回答

0

考慮閱讀文檔。

ExecuteSqlCommandAsync

不返回SP中的選定值作爲返回值。它返回一個錯誤代碼 - SP。你從未設置過。這是一個整數。

您需要打開數據讀取器或使用標量版本的execute(它返回第1行第1列)以獲取GUID。

的文檔上顯然清楚:

Return Value Type: System.Threading.Tasks.Task<Int32> A task that represents the asynchronous operation. The task result contains the result returned by the database after executing the command.

這表明你從來沒有看過。

+0

是真的,我從來沒有讀過它,它只是我試圖存檔的一個示例,我希望有人可以建議一個不同的context.com和可以操作這個...正如我所說這對我來說是新的。無論如何,你的態度很糟糕! – Eyal 2014-12-03 10:11:46

+0

我同意。我態度糟糕。我希望程序員閱讀文檔。我希望餐廳的食物能夠專業製作。我對在編程中從事金錢工作的人感到厭惡,因爲他們從不去閱讀文檔。你是對的。可怕。 – TomTom 2014-12-03 10:13:16

+0

它讓你知道我的整個故事,我正在爲編程賺錢......真正的故事是我今天需要給另一位正在開始創業的程序員提供API,正如我所說這對我來說是新的。你知道,有時你需要靈活而快速,這就是我尋求幫助的原因。但你...你可以保持閱讀文檔。 – Eyal 2014-12-03 10:41:57

相關問題