0

我按照微软在此处提供的 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'."}

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

4

1 回答 1

0

我认为您需要将 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-28T11:57:25.083 回答