插入后获得结果ID

时间:2013-05-05 09:18:51

标签: c# azure windows-phone azure-mobile-services

假设我有下表:

 public class SomeItem
 {
    public int Id { get; set; }

    [DataMember(Name = "text")]
    public string Text { get; set; }
 }

我可以轻松地做一些事情:

var items = await MobileService.GetTable<SomeItem>.Where(x=>x.Id > 50).ToListAsync();
//print items here

但是一旦插入结果项目,我就无法找到获得结果项目的方法。例如,我可能需要来自SomeItem的ID。这是我希望能够做到的:

var item = await MobileService.GetTable<SomeItem>.Insert(new SomeItem{Text="hi"}).Result;

2 个答案:

答案 0 :(得分:2)

您可以单独创建要插入的项目,以便在插入后可以使用它的引用:

var newItem = new SomeItem{Text="hi"};
await MobileService.GetTable<SomeItem>.Insert(newItem); // Or whatever syntax you need here!
// Now you can use newItem after it's been inserted (and the 'Id' key has been updated by the insert)

答案 1 :(得分:0)

(目前)测试版客户端SDK中存在重大变化。请参阅此帖子的详细信息:

http://blogs.msdn.com/b/carlosfigueira/archive/2013/03/14/azure-mobile-services-managed-client-upcoming-breaking-changes.aspx

在答案中提供的代码中,由于“SomeItem”是一种已知的数据类型,我认为行为将保持不变,并且对象将被修补到位。如果您使用的是未键入的数据(JSON),则不会对该对象进行修补。您必须依赖返回的对象来检查id的值。卡洛斯在上面的帖子中更详细地解释了这一点。

内特

相关问题