【问题标题】:Windows Azure Mobile Service: InsertAsyncWindows Azure 移动服务:InsertAsync
【发布时间】:2013-06-28 09:57:12
【问题描述】:

我遵循了 Microsoft 通过here 提供的 Windows Azure 移动服务指南。

我创建了一个category 类,它表示如下类别表:

public class category
    {
        public int Id { get; set; }

        //// TODO: Add the following serialization attribute.
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        //// TODO: Add the following serialization attribute.
        [JsonProperty(PropertyName = "subscribers")] //Number of Subscribers
        public int Subscribers { get; set; }

        [JsonProperty(PropertyName = "posts")] //Number of posts inside this category
        public int Posts { get; set; }
    }

然后我将一个条目插入到数据库中:

private IMobileServiceTable<category> categoryTable = App.MobileService.GetTable<category>();
category temp = new category() { Name = "test", Posts = 1, Subscribers = 2 };
            await categoryTable.InsertAsync(temp);

到这里为止一切正常。然后我创建了一个users 类来表示用户表,如下所示:

class users
    {
        public int Id { get; set; } //the generated ID by the mobile service.

        //// TODO: Add the following serialization attribute.
        [JsonProperty(PropertyName = "name")]
        public string Name { get; set; }

        //// TODO: Add the following serialization attribute.
        [JsonProperty(PropertyName = "nusnet")]
        public string NUSNET { get; set; }

        [JsonProperty(PropertyName = "email")]
        public string Email { get; set; }

        [JsonProperty(PropertyName = "faculty")]
        public string Faculty { get; set; }

    }

现在当我尝试添加用户时:

await userTable.InsertAsync(loggedInUser);

其中登录用户是用户的详细信息。如指南中所述,我将 Id 字段留空,在调试期间我注意到 Id 设置为 0。

我收到一个错误:

NewtonSoft.JSON.JsonSerializationException: {"Error getting value from 'Id' on 'NUSocial.users'."}

我一直在尝试解决这个问题,但我不知道出了什么问题。

【问题讨论】:

  • 您是在 Windows Phone 8 还是 Windows Phone 7.x 上使用它?对于 WP7.x,该类需要公开(将其设为 public class users 而不是您当前拥有的)。
  • 非常感谢!它就像一个魅力。很高兴看到 MS 工程师在这些网站上提供帮助 :)

标签: c# json azure windows-phone azure-mobile-services


【解决方案1】:

我认为您需要将 JSON 属性应用于 Id 属性。这是我的代码的示例,做同样的事情,看起来像:

[DataContract]
public class Scores
{
    [JsonProperty(PropertyName = "id")]
    [DataMember]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "UserName")]
    [DataMember]
    public string UserName { get; set; }

...

                await scoresTable.InsertAsync(new Scores
                        {
                            UserName = _userName,
                            Score = (int) Score
                        });

【讨论】:

    猜你喜欢
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    相关资源
    最近更新 更多