Azure移动服务中的更新表 [英] update table in Azure Mobile Service

查看:55
本文介绍了Azure移动服务中的更新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Azure移动服务中更新特定记录.

例如,我有一个名为 country 的天蓝色表,其中有两列

How to update particular record in azure mobile Service.

For Example I have a table in azure called country having two columns

  • country_id
  • 国家名
  • country_id
  • country_name

如果我想用country_id=5将记录从美国更新到美国.如何执行此操作.

If I want to update the record with country_id=5 from USA to United State of America. How to Perform this.

//Global Variable  
  private MobileServiceCollection<country, country> items;
        private IMobileServiceTable<country> todoTable = App.MobileService.GetTable<country>();
    class country
    {
    public string id { get; set; }
    public string country_name { get; set; }
    public int country_id { get; set; }
    }

private async void btnUpdate_Click(object sender, RoutedEventArgs e)
 {
var change = await todoTable
 .Where(todoItem => todoItem.country_name == tboxcName.Text)
    .ToListAsync();
  await todoTable.UpdateAsync(change);
    }

我在帖子中尝试了上述代码,但确实找不到.

The above code I tried from this post, but did not find out.

推荐答案

您可能想尝试一下:

private async void btnUpdate_Click(object sender, RoutedEventArgs e)
 {
var change = await todoTable
 .Where(todoItem => todoItem.id.Equals(tboxcName.Text))
    .ToListAsync();

    if(change != null){
        var toChange= change.First();
        toChange.country_name="United State of America";
          await todoTable.UpdateAsync(toChange);
    }
    else{
    // you have nothing to change, you might throw an exception
    }
    }

在文本框中,您应该输入要更新的ID(以您的情况为5).然后,我进行一次linq查询,选择所有ID为"5"的项目,这将得到一个列表. 我检查列表是否不为空,如果列表为空,您将要威胁这种情况.如果它不为null,则我采用第一个元素(就像您在移动服务中一样,id字段在数据库中是唯一的,因此您不必威胁这种边界情况(尽管如果您确实想确保它总是更好,做到)). 列表中的第一个元素的ID为5,因此我们将对象的国家/地区名称更改为美国"(在您的情况下,您无需考虑先前的值,因为您正在更新特定的ID).然后,您更新此项目.然后,移动服务API将发出补丁请求,数据库将根据对象ID对其进行补丁.

In the textbox you should enter the id you want to update, in your case it's 5. Then I make a linq query selecting all the items with Id "5", this gets me a list. I check if the list is not void, you would want to threat the case when the list is null. If it's not null I take the first element (as you are in the mobile services the id field is unique in the database, so you don't have to threat this boundary case (although if you really want to be sure it's always better to do it)). The first element in the list will have the ID=5, so we change the object updating it's country name to "United States of America" (in your case, you don't care of the previous value, as you are updating a specific ID). Then you update this item. The mobile service API will then issue a patch request, and the database will patch it according to the Object ID.

希望这会有所帮助!

这篇关于Azure移动服务中的更新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆