离线时未分配 Azure 移动服务标识列

Azure Mobile Services Identity Column Not Assigned While Offline

我将 Azure 移动服务 .NET 后端与 Xamarin.Forms 一起使用,但在使用 int 身份 ID 列时遇到问题。在离线情况下,我可以成功地将记录添加到数据库中,但作为身份的 CustomerId 列仍为 0。一旦在线,当我同步时,该值由 SQL 服务器设置并填充到移动设备。

由于需要 ID,因为我还需要在另一个 table 中创建相关数据,问题是,我如何才能 get/set 在实体与服务器?我无法手动设置它,因为它可能会与另一个客户端发生冲突。我正在起诉现有的 SQL 数据库,因此我宁愿不必完全更改方案来更改 ID 以使用字符串或 guid。

    public class Customer
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int CustomerId { get; set; }

        public string Name { get; set; }

        ....
    }

    public async Task SaveCustomer(Customer item) {
        await CustomerTable.InsertAsync (item);
    }

您提到的场景正是 Azure 移动应用程序使用字符串作为 ID 的原因。如果您使用字符串,您可以为 ID 生成一个 GUID 并在同步之前使用它。

获取数据库生成的 Id 的唯一方法是同步 table。