如何在一个请求中更新OData实体并修改其导航属性? [英] How do I update an OData entity and modify its navigation properties in one request?

查看:184
本文介绍了如何在一个请求中更新OData实体并修改其导航属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WCF数据服务提供的OData服务(我现在使用OData V3应用程序/json; odata = verbose有效负载格式)来实现我认为简单的方案.我可能会在其中使用JSON Light格式未来).基本情况如下:

I am trying to implement what I thought was a simple scenario using an OData service provided by WCF Data services (using the OData V3 application/json;odata=verbose payload format, for now. I may use the JSON Light format in the future). The basic scenario goes like this:

我有两个实体:

class Person 
{ 
  public int ID { get; set; }
  public string Name { get; set; } 
  public virtual PersonCategory Category { get; set; }
}

class PersonCategory
{
  public int ID { get; set; }
  public string Description { get; set; }
  public virtual ICollection<Person> People { get; set; }
}

现在,我想为一个人创建一个简单的编辑页面.此编辑页面可能有名称"的输入,以及人的类别"的输入或下拉菜单.

Now, I want to create a simple edit page for a Person. This edit page might have an input for the Name, and an input or drop-down for the Category of the Person.

因此,情况发生了:

  1. 代码使用$ expand下载人员,用于类别:GET/api.svc/People(1)?$ expand = Category
  2. 用户同时编辑人员的姓名"属性及其类别.
  3. 该页面的代码发出单个请求以更新该人员的姓名"和类别"属性.

此处的键位于单个请求"中.这是我无法找到文档的部分.我已经看到了将上面的数字3分为两个请求的示例.这样的事情(我不记得确切的格式-我也不确定在执行PUT之前是否必须删除Category链接):

The key here is in "a single request". This is the part that I'm having trouble finding documentation for. I've seen examples where they split number 3 above into two requests. Something like this (I don't remember the exact format - I'm also not sure if you'd have to DELETE the Category link before doing the PUT):

PATCH /api.svc/People(1) with content: {"Name": "new name" }
PUT /api.svc/People(1)/$links/Category with content: { "url": "/api.svc/Categories(2)" }

但是,我也听说过它,但没有得到证实,可以通过将对类别导航属性的更改与对Person实体的其他更改内联地指定为一个实例来实现此更新为单个请求.有人可以给我举个例子吗?另外,您能告诉我如何使用多对多导航属性来完成此操作,而不是上文所述的一对多导航.

But, I've also heard it said, but not demonstrated, that it's possible to implement this update as a single request with the change to the Category navigation property specified inline with the other changes to the Person entity. Could someone give me an example of how this might be done? Also, can you show me how it would be done with a many-to-many navigation property, as opposed to the one-to-many I've described above.

最后,我目前正在使用详细的JSON格式V3.如果我改用新的JSON light格式,您对上述问题的答案会有所不同吗?如果可以,怎么办?

And finally, I'm currently using the verbose JSON format, V3. Would your answers to the questions above be different if I instead used the new JSON light format? If so, how?

推荐答案

普拉蒂克(Pratik)的评论就是答案(普拉蒂克(Pratik)如果您想将其重新发布为答案,我会这样标记-谢谢!):

Pratik's comment was the answer (Pratik if you'd like to repost this as an answer, I'll mark it as such - thanks!):

问题:您要更新类别实例还是要更新类别实例的某些属性.除了批处理之外,没有其他方法可以做.对于前者,您可以执行以下操作:{"Name":"new name","Category":{"__metadata":{"uri":"/api.svc/Categories(2)"}}}}.希望这可以帮助. – Pratik

Question: Do you want to update the category instance or do you want to update some of the properties of the category instance. There is no way to do the later other than batch. For the former, you can do something like: { "Name" : "new name", "Category" : { "__metadata" : { "uri" : "/api.svc/Categories(2)" }}}. Hope this helps. – Pratik

这篇关于如何在一个请求中更新OData实体并修改其导航属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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