How do I update an OData entity and modify its navigation properties in one request?

跟風遠走 提交于 2019-12-18 13:22:37

问题


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:

I have two entities:

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.

So, the scenario goes:

  1. Code downloads the Person using $expand for the Category: GET /api.svc/People(1)?$expand=Category
  2. User edits both the person's Name property and their Category.
  3. Code for the page makes a single request to update that Person's Name and Category properties.

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)" }

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.

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?


回答1:


I found two ways to represent navigation properties inline:

application/json;odata=verbose - { "Name": "new name", "Category": { "__metadata": { "uri": "Categories(2)" }}}

application/json - { "Name": "new name", "Category@odata.bind": "Categories(2)" }




回答2:


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

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




回答3:


You don't need a batch, anymore. You can do it in one call. You simply need to also send up the changed properties and let the repository handle the changed properties.

public class Person 
{
   public string FirstName {get;set;}
   public string LastName {get;set;}
   public int Age {get;set;}
}

Let's say you notice the first name has a typo, Jhon and it is supposed to be John. You can edit the first name and send it up. So you have the following object model. You can get this in 1 of two ways:

  • Have two parameters and set BodyStyle = WebMessageBodyStyle.Wrapped
  • Just create a generic model object with two properties: Property one is of Type T and property 2 is a List.

So you would send up this json:

[{ FirstName = 'John' }, ['FirstName']]

Now on the server side, you can do what you need to do.

If you don't want to send the changed properties, you can guess the changed properties by choosing any property whose value isn't the default property.

{ FirstName = 'John' }

Then you can use a few methods to see what properties have changed:

  • Custom code per entity to make sure that each property is not the default value and set it. Requires a piece of code per entity.
  • Reflection to make sure that each property is not the default value. Requires 1 class for all entities. I did this back in 2014 here in Entity Framework: http://www.rhyous.com/2014/12/01/entityupdater-generic-helper-for-entity-framework/


来源:https://stackoverflow.com/questions/12604673/how-do-i-update-an-odata-entity-and-modify-its-navigation-properties-in-one-requ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!