c# - 如何使用 Azure Mobile 的 .NET 客户端获取新插入项目的 Id 字段

标签 c# .net azure azure-sql-database azure-mobile-services

我正在尝试使用 Azure 移动服务为异步多人游戏创建后端。我在 WAMS 上使用 SQL 数据库和 .NET 后端,从 .NET 客户端(Xamarin.iOS 特别是 atm)调用服务。

数据库中项目的类:

public class Match {
    public string Id { get; set; }
    public int Challengers { get; set; }
    string GameData { get; set; }
    public List<string> Players { get; set; }
    public string LastPlayer { get; set; }
    public string Message { get; set; }
    public string NextPlayer { get; set; }
    public int PlayerGroup { get; set; }
}

我使用以下方法将其插入数据库:

var matchtable = MobileService.GetTable <Match> ();
CurrentMatch = new Match {
                Message = variant.ToString () + ", " + CurrentUser + " vs ??",
                NextPlayer = CurrentUser,
                Players = players,
                PlayerGroup = playerGroup,
                Challengers = 0,
                Game = null,
                LastPlayer = null
            };
await matchtable.InsertAsync (CurrentMatch);

然后我正在做其他会影响比赛的事情,并且需要稍后再次更新,但我没有 CurrentMatch 能够进行更新的 Id 字段。我能找到的所有内容都告诉我,我应该在插入后取回 Id 字段(方法返回某些内容或用它更新 CurrentMatch 本身),但它必须全部谈论 javascript 后端或不同的客户端或其他内容。 .NET 客户端中的 InsertAsync 方法没有返回值(从技术上讲,返回 Task),并且 CurrentMatch 不会使用调用中的 Id 字段进行更新(这也是有道理的,因为它不是 ref 或 out 参数)。

我到底应该如何获取刚刚插入数据库的对象的 Id 字段?

最佳答案

我假设您正在使用最新版本的移动服务客户端 SDK,在这种情况下您将调用此 InsertAsync method here

你说得对,该参数不是 ref 或 out 参数,但它可以修改你传入的对象的字段。在这种情况下,它将修改 Match 对象的内容。

我的猜测是还有另一个代码问题在干扰。或者,如果该代码片段位于方法中,请确保它返回一个任务并在检查 Id 的内容之前等待它。一个简单的控制台日志应该会有所帮助。

如果这不能解决问题,那么请包含更多上下文,否则您编写的代码应该按照我所说的那样运行。

关于c# - 如何使用 Azure Mobile 的 .NET 客户端获取新插入项目的 Id 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30749143/

相关文章:

c# - 在 Discord.net 中连接到语音 channel 并发送音频

c# - 为什么我需要使用 Activator CreateInstance?

c# - JavaScript 或 C#

c# - 确定变量使用的字节数

c# - Azure 中同时使用 ASP.NET + PHP

azure - 为 kubernetes 负载均衡器分配一个来自内部网络的 IP

c# - 识别原始打印机

c# - winform之间传递变量

c# - 无论顺序如何,获取字符串列表的哈希值

c# - Azure服务总线,如何以先进先出的顺序接收 session ? (先进先出)