简体   繁体   English

Windows Azure移动服务:InsertAsync

[英]Windows Azure Mobile Service: InsertAsync

I followed the Windows Azure mobile service guide given by Microsoft over here . 这里,我遵循了Microsoft提供的Windows Azure移动服务指南。

I create a category class which represented the category table as follows: 我创建一个表示类别表的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; }
    }

I then inserted an entry into the database as give: 然后,我将一个条目插入到Give中:

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

All worked fine till here. 到这里一切都很好。 Then i created a users class to represent the users table as follows: 然后,我创建了一个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; }

    }

Now when I try to add in a user: 现在,当我尝试添加用户时:

await userTable.InsertAsync(loggedInUser);

where logged in user is the details of the user. 登录的用户是用户的详细信息。 As given in the guide, i leave the Id filed empty and during debugging I notice that the Id is set as 0. 如指南中所述,我将ID字段保留为空,并且在调试过程中注意到ID设置为0。

I get an error: 我收到一个错误:

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

I have been trying fixing this for a while now but I have no clue on what is going wrong. 我已经尝试修复了一段时间,但是对于出什么问题我一无所知。

I think you'll need to apply the JSON attribute to the Id property. 我认为您需要将JSON属性应用于Id属性。 This is a sample of what my code, doing the same thing, looks like: 这是我的代码执行相同操作的示例,如下所示:

[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
                        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM