Windows Azure移动服务:InsertAsync

Windows Azure Mobile Service: InsertAsync
2021-07-22
  •  译文(汉语)
  •  原文(英语)

这里,我遵循了Microsoft提供的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; }
    }

然后,我将一个条目插入到Give中:

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'."}

我一直在尝试修复此问题已有一段时间,但是对于出问题的原因我一无所知.

速聊1:
您是否将其用于Windows Phone 8或Windows Phone 7.x?对于WP7.x,该类需要是公共的(使它public class users代替您当前的类).
速聊2:
太感谢了!它像魅力一样运作.很高兴看到MS工程师在此类网站上提供帮助:)
解决过程1

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

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.

Talk1:
Are you using it for Windows Phone 8 or Windows Phone 7.x? For WP7.x, the class needs to be public (make it public class users instead of what you currently have).
Talk2:
Thank you so much! It worked like a charm. Great to see MS engineers helping out on such sites :)
Solutions1

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
                        });
转载于:https://stackoverflow.com/questions/17361995/windows-azure-mobile-service-insertasync

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入