如何更新azure移动服务中的特定记录。
例如,我在azure中有一个名为 country 的表,有两列
如果我想用country_id=5
从美国更新到美国的记录。如何执行此操作。
//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);
}
上面的代码我尝试了post,但没有找到。
答案 0 :(得分:1)
您可能想尝试一下:
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对其进行修补。
希望这有帮助!
答案 1 :(得分:0)
尝试使用此C#代码!
private async void btnUpdate_Click(object sender, RoutedEventArgs e)
{
var filteredRecords = await todoTable.Where(
todoItem => todoItem.country_id == 5)
.ToListAsync();
foreach (var item in filteredRecords)
{
item.country_name="United States of America";
await todoTable.UpdateAsync(item);
}
}