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



我刚刚开始使用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

考虑阅读文档。

ExecuteSqlCommandAsync

不返回 SP 中的选定值作为返回值。它返回 SP 的错误代码 - 。你从未设置过。这是一个整数。

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

文档显然很清楚:

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

这表明您从未阅读过它。

最新更新