Windows Azure移动服务:InsertAsync

本文关键字:InsertAsync 服务 移动 Azure Windows | 更新日期: 2023-09-27 18:20:26

我遵循了微软在这里提供的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类来表示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 Azure移动服务:InsertAsync

我认为您需要将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
                        });