使用C#后端的移动服务:返回Guid

时间:2014-12-03 08:58:19

标签: c# azure

我刚开始使用Azure的“移动服务”,我使用C#作为支持。我正在尝试将Guid返回给客户端,但我收到错误:“Cannot convert source type int to target type 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

1 个答案:

答案 0 :(得分:0)

考虑阅读文档。

  

ExecuteSqlCommandAsync

不会将SP中的选定值作为返回值返回。它返回SP的错误代码。你从未设定过的。这是一个int。

您需要打开数据阅读器或使用标量版本的execute(返回第1行第1列)来获取GUID。

文档显然很清楚:

  

返回值类型:System.Threading.Tasks.Task&lt; Int32&gt;一项任务   表示异步操作。任务结果包含   执行命令后数据库返回的结果。

表示你从未读过它。