2013-06-28 86 views
0

我遵循Microsoft提供的有关here的Windows Azure移动服务指南。Windows Azure移动服务:插入同步

我创造它代表类别表如下:a 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'."} 

我一直在尝试了一段时间修复这个现在,但我不知道发生了什么问题。

+3

你使用它的Windows Phone 8或Windows Phone 7.x的?对于WP7.x,该类需要公开(使其成为“公用类用户”而不是您目前拥有的)。 – carlosfigueira

+0

非常感谢!它像一个魅力。很高兴看到MS工程师在这样的网站上提供帮助:) – Saurabh

回答

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 
         }); 
相关问题