{"id":1083,"date":"2010-11-11T10:29:49","date_gmt":"2010-11-11T10:29:49","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/odatateam\/2010\/11\/11\/introduction-to-multi-valued-properties\/"},"modified":"2024-02-16T13:28:39","modified_gmt":"2024-02-16T20:28:39","slug":"introduction-to-multi-valued-properties","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/odata\/introduction-to-multi-valued-properties\/","title":{"rendered":"Introduction to Multi-Valued Properties"},"content":{"rendered":"<p class=\"MsoNoSpacing\"><strong><span lang=\"EN\">Although you may see both the terms, \u201cBags\u201d and \u201cMulti-Valued Property\u201d, they refer to the same thing. While this naming change was not included with the latest WCF Data Services 2010 October CTP release, based on community feedback, we\u2019ve decided to use Multi-Valued Property for all future releases. <\/span><\/strong>    <\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\"><\/span><\/strong><\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\">What is a Bag Type?<\/span><\/strong> <\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span lang=\"EN\">Today entities in WCF Data Services (Customer, Order, etc) may have properties whose value are primitive types, complex types or represent a reference to another entry or collection of entries. However there is no support for properties that *contain* a collection (or Bag) of values, the introduction of this type is meant to support such scenarios. Protocol changes need to support Bag type can be found <\/span><a href=\"https:\/\/www.odata.org\/blog\/2010\/9\/27\/adding-support-for-bags\"><span lang=\"EN\"><font color=\"#0000ff\">here<\/font><\/span><\/a><span lang=\"EN\"> <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\"><\/span><\/strong><\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\">How can I specify a Bag property in CSDL?<\/span><\/strong><span style=\"font-size: 9pt\"> <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span>As you know, WCF Data Services may expose a <\/span><a href=\"https:\/\/www.odata.org\/developers\/protocols\/overview#MetadataForODataServices\"><span><font color=\"#0000ff\">metadata document<\/font><\/span><\/a><span> ($metadata endpoint) which describes the data model exposed by the service.&#160; Below is an example of the&#160; extension of the metadata document&#160; to define Bag properties.&#160;&#160; In this case, the Customer Entry has a Bag of email addresses and apartment addresses. <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span><a href=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2010\/11\/1512.clip_image001_thumb_1.jpg\"><img decoding=\"async\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" title=\"clip_image001\" border=\"0\" alt=\"clip_image001\" src=\"https:\/\/devblogs.microsoft.com\/odatateam\/wp-content\/uploads\/sites\/23\/2010\/11\/1512.clip_image001_thumb_1.jpg\" width=\"607\" height=\"476\" \/><\/a><\/span><span lang=\"EN\"> <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\"><\/span><\/strong><\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\">How are Bags represented on the client?<\/span><\/strong><span lang=\"EN\"> <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span lang=\"EN\">From a WCF Data Services Client perspective a Bag property is an ICollection&lt;T&gt;, where in the above example T is SampleModel.Address or SampleModel.String. When using \u201cAdd Service Reference\u201d against WCF Data Service in Visual Studio, WCF Data Services will generate an ObservableCollection&lt;T&gt; for each Bag Property in the model. If using DataSvcUtil.exe without specifying the \u201c\/DataServiceCollection\u201d flag, System.Collections.ObjectModel.Collection&lt;T&gt; will be used to represent Bag properties of the entity type <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span lang=\"EN\">A representation of the above example in a .NET type would be: <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160; <span style=\"color: blue\">class<\/span> <span style=\"color: #2b91af\">Customer<\/span> <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160; { <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: blue\">int<\/span> ID { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; } <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: blue\">string<\/span> Name { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; } <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">ICollection<\/span>&lt;<span style=\"color: #2b91af\">Address<\/span>&gt; Addresses { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; } <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span style=\"color: #2b91af\">ICollection<\/span>&lt;<span style=\"color: blue\">string<\/span>&gt; EmailAddresses { <span style=\"color: blue\">get<\/span>; <span style=\"color: blue\">set<\/span>; } <\/span><\/p>\n<\/p>\n<\/p>\n<p class=\"MsoNormal\"><span style=\"font-family: consolas;font-size: 9.5pt\">&#160;&#160;&#160; } <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\"><\/span><\/strong><\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\">How are Bags used on the client?<\/span><\/strong><span lang=\"EN\"> <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span lang=\"EN\">Bag is like any other complex type in the system, in terms of client CRUD operations with the notable exception that Bag properties cannot be null, they can only be empty. <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span lang=\"EN\">NorthwindEntities svc = new NorthwindEntities(new Uri(&quot;<\/span><span lang=\"EN\">http:\/\/foobar\/Northwind.svc\/&quot;));<\/span><span lang=\"EN\">      <br \/><\/span><span>svc.Customers.Select<\/span><span> <\/span><span>(c =&gt; new { Addresses= c.Addresses}) <\/span><\/p>\n<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><span lang=\"EN\"><\/span><\/p>\n<p>&#160;<\/p>\n<\/p>\n<p style=\"line-height: 13.5pt;background: white\"><strong><span lang=\"EN\">Ahmed Moustafa<\/span><\/strong><span lang=\"EN\">      <br \/>Program Manager       <br \/>WCF Data Services <\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Although you may see both the terms, \u201cBags\u201d and \u201cMulti-Valued Property\u201d, they refer to the same thing. While this naming change was not included with the latest WCF Data Services 2010 October CTP release, based on community feedback, we\u2019ve decided to use Multi-Valued Property for all future releases. What is a Bag Type? Today entities [&hellip;]<\/p>\n","protected":false},"author":512,"featured_media":3253,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-odata"],"acf":[],"blog_post_summary":"<p>Although you may see both the terms, \u201cBags\u201d and \u201cMulti-Valued Property\u201d, they refer to the same thing. While this naming change was not included with the latest WCF Data Services 2010 October CTP release, based on community feedback, we\u2019ve decided to use Multi-Valued Property for all future releases. What is a Bag Type? Today entities [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/1083","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\/512"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/comments?post=1083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/posts\/1083\/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=1083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/categories?post=1083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/odata\/wp-json\/wp\/v2\/tags?post=1083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}