{"id":333,"date":"2014-04-09T22:15:00","date_gmt":"2014-04-09T22:15:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2014\/04\/09\/tutorial-sample-client-property-tracking-for-patch\/"},"modified":"2020-01-07T07:38:34","modified_gmt":"2020-01-07T14:38:34","slug":"tutorial-sample-client-property-tracking-for-patch","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/tutorial-sample-client-property-tracking-for-patch\/","title":{"rendered":"[Tutorial &amp; Sample] Client Property Tracking for PATCH"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p>In OData Client for .NET 6.2.0, we enabled the top level property tracking on client side when sending a PATCH. This feature allows client to send only updated properties, instead of the entire object to server.<\/p>\n<p>The top level property tracking means the client will only track the highest level properties in the class hierarchy. In other words, if customer is updating a property under a complex type property, or an item in a collection property under an entity, the client will send the entire complex type property (including unchanged properties under the complex property) or the entire collection (including unchanged items in the collection) to the server.<\/p>\n<p>In implementing property tracking, we start by creating a DataServiceCollection instance. DataServiceCollection helps to track all the properties. For more information about DataServiceCollection, See \u201c<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ee373844(v=vs.110).aspx\">Binding Data to Controls (WCF Data Services)<\/a>\u201d.<\/p>\n<p>Let\u2019s move to a real example to explain how to do it.<\/p>\n<h3>Service EDM model<\/h3>\n<p><a href=\"http:\/\/services.odata.org\/V4\/(S(readwrite))\/OData\/OData.svc\/\">http:\/\/services.odata.org\/V4\/(S(readwrite))\/OData\/OData.svc\/<\/a><\/p>\n<p>Following sample code will base on this model.<\/p>\n<h3>Client Code<\/h3>\n<p>You can read \u201c<a href=\"http:\/\/blogs.msdn.com\/b\/odatateam\/archive\/2014\/03\/11\/how-to-use-odata-client-code-generator-to-generate-client-side-proxy-class.aspx\">How to use OData Client Code Generator to generate client-side proxy class<\/a>\u201d for generating client-side proxy class.<\/p>\n<p>Now, we will show you <strong>how to enable property tracking<\/strong> when doing a PATCH.<\/p>\n<p><strong>1. <\/strong>We need a DataServiceCollection which will handle property tracking.<\/p>\n<p style=\"padding-left: 30px;\"><em><span style=\"font-family: courier new,courier;\">DataServiceCollection&lt;Product&gt; products = <\/span><span style=\"font-family: courier new,courier;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\"><span style=\"color: #0000ff; font-size: x-small;\">new<\/span><\/span><\/span><span style=\"font-size: x-small;\"><span style=\"font-size: x-small;\"> DataServiceCollection&lt;Product&gt;(dataServiceContext.Products.Where(p =&gt; p.ID == 0));<\/span><\/span><\/span><\/em><\/p>\n<p><strong>2. <\/strong>Update the property<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: courier new,courier;\"><em>products[0].Price = 3.0;<\/em><\/span><\/p>\n<p><strong>3. <\/strong>Save changes<\/p>\n<p>Http-Method will be \u201cPATCH\u201d when updating with SaveChangesOptions.None. This step will send the updated top level properties.<\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: courier new,courier;\"><em>dataServiceContext.SaveChanges();<\/em><\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: courier new,courier;\">or<\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"font-family: courier new,courier;\"><em>dataServiceContext.SaveChanges(SaveChangesOptions.None);<\/em><\/span><\/p>\n<h3>Sample Code<\/h3>\n<p>Here I will provide several samples with their payloads.<\/p>\n<p>1. Update top-level primitive property. Only the updated primitive property will be sent.<\/p>\n<p><strong><em>Sample Code:<\/em><\/strong><\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/49cce872b6cd2dd746bb.js\"><\/script><\/p>\n<p><strong><em>Payload:<\/em><\/strong><\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/0c569d468dca2237add8.js\"><\/script><\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>2. <\/em><\/strong>Update property in top-level complex property. The entire complex property (\u201cAddress\u201d in the example) instead of the updated property (\u201cCity\u201d in the example) in the complex property will be sent to server.<\/p>\n<p><strong><em>Sample Code:<\/em><\/strong><\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/e16f5a53c1e785565f6c.js\"><\/script><\/p>\n<p><strong><em>Payload for complex type:<\/em><\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/c65095ae2dda24b4fe6b.js\"><\/script><\/p>\n<p>3. Update a navigation entity, only the updated property of the entity will be sent out.<\/p>\n<p><strong><em>Sample code:<\/em><\/strong><\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/0d7ebfc5a210e2ea65e0.js\"><\/script><\/p>\n<p><strong><em>Payload:<\/em><\/strong><\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/19fde5801bb8002c0a06.js\"><\/script><\/p>\n<h3>Note<\/h3>\n<p>If you don\u2019t use DataServiceCollection, then client will not support property tracking. For example:<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/b31a259e5c81c03b06ca.js\"><\/script><\/p>\n<p>Although client tracked the entity after ToList() was called. SaveChanges() will not send any request. The client cannot track the property change.<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/266102e8637d96c4485d.js\"><\/script><\/p>\n<p>If you tried to use following code to update the entity, then the whole entity will be sent, instead of the only updated properties.<\/p>\n<p>Payload will be like<\/p>\n<p><script type=\"text\/javascript\" src=\"https:\/\/gist.github.com\/ODataTeam\/9c20c09b1768af5f2d86.js\"><\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; In OData Client for .NET 6.2.0, we enabled the top level property tracking on client side when sending a PATCH. This feature allows client to send only updated properties, instead of the entire object to server. The top level property tracking means the client will only track the highest level properties in the class [&hellip;]<\/p>\n","protected":false},"author":518,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[50],"class_list":["post-333","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata","tag-odata-client-tools"],"acf":[],"blog_post_summary":"<p>&nbsp; In OData Client for .NET 6.2.0, we enabled the top level property tracking on client side when sending a PATCH. This feature allows client to send only updated properties, instead of the entire object to server. The top level property tracking means the client will only track the highest level properties in the class [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/333","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/users\/518"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=333"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/333\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media\/3253"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/media?parent=333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}