Introduction to Multi-Valued Properties

DB Blogs

Although you may see both the terms, “Bags” and “Multi-Valued Property”, 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’ve decided to use Multi-Valued Property for all future releases.

What is a Bag Type?

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 here

How can I specify a Bag property in CSDL?

As you know, WCF Data Services may expose a metadata document ($metadata endpoint) which describes the data model exposed by the service.  Below is an example of the  extension of the metadata document  to define Bag properties.   In this case, the Customer Entry has a Bag of email addresses and apartment addresses.


How are Bags represented on the client?

From a WCF Data Services Client perspective a Bag property is an ICollection<T>, where in the above example T is SampleModel.Address or SampleModel.String. When using “Add Service Reference” against WCF Data Service in Visual Studio, WCF Data Services will generate an ObservableCollection<T> for each Bag Property in the model. If using DataSvcUtil.exe without specifying the “/DataServiceCollection” flag, System.Collections.ObjectModel.Collection<T> will be used to represent Bag properties of the entity type

A representation of the above example in a .NET type would be:

    class Customer


        int ID { get; set; }

        string Name { get; set; }

        ICollection<Address> Addresses { get; set; }

        ICollection<string> EmailAddresses { get; set; }


How are Bags used on the client?

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.

NorthwindEntities svc = new NorthwindEntities(new Uri("http://foobar/Northwind.svc/"));
svc.Customers.Select (c => new { Addresses= c.Addresses})


Ahmed Moustafa
Program Manager
WCF Data Services


Discussion is closed.

Feedback usabilla icon