我按照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
类来表示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'."}
我一直试图解决这个问题一段时间,但我不知道出了什么问题。
答案 0 :(得分: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
});