【问题标题】:How do you get the Id field for a newly inserted item using the .NET client for Azure Mobile如何使用 Azure Mobile 的 .NET 客户端获取新插入项目的 Id 字段
【发布时间】:2015-06-10 06:55:39
【问题描述】:

我正在尝试使用 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 字段?

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2020-09-04
      • 2012-12-03
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      相关资源
      最近更新 更多