简体   繁体   中英

Windows Azure Mobile Service: InsertAsync

I followed the Windows Azure mobile service guide given by Microsoft over here .

I create a category class which represented the category table as follows:

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:

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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