EntityPropertyMappingAttribute Class

Attribute that specifies a custom mapping between properties of an entity type and elements of an entry in an Open Data Protocol (OData) feed returned by the data service. 

System.Object
  System.Attribute
    System.Data.Services.Common.EntityPropertyMappingAttribute

Namespace:  System.Data.Services.Common
Assembly:  Microsoft.Data.OData (in Microsoft.Data.OData.dll)

[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EntityPropertyMappingAttribute : Attribute

The EntityPropertyMappingAttribute type exposes the following members.

  NameDescription
Public methodEntityPropertyMappingAttribute(String, SyndicationItemProperty, SyndicationTextContentKind, Boolean)Creates a new instance of the EntityPropertyMappingAttribute.
Public methodEntityPropertyMappingAttribute(String, String, String, String, Boolean)Creates an instance of the EntityPropertyMappingAttribute to map a property to a custom feed element.
Top

  NameDescription
Public propertyKeepInContentGets a Boolean value that indicates whether a property value should be repeated both in the content section of the feed and in the mapped location.
Public propertySourcePathGets the name of the property of the syndication item that will be mapped to the specified element of the feed.
Public propertyTargetNamespacePrefixGets a string value that, together with TargetNamespaceUri, specifies the namespace in which the TargetPathelement exists.
Public propertyTargetNamespaceUriGets a string value that specifies the namespace URI of the element specified by the TargetPath property.
Public propertyTargetPathGets the name of the custom target in the feed to which the property is mapped.
Public propertyTargetSyndicationItemGets the syndication item in the entry targeted by the mapping.
Public propertyTargetTextContentKindGets the type of content of the property mapped by EntityPropertyMappingAttribute.
Public propertyTypeId (Inherited from Attribute.)
Top

  NameDescription
Public methodEquals (Inherited from Attribute.)
Public methodGetHashCode (Inherited from Attribute.)
Public methodGetType (Inherited from Object.)
Public methodIsDefaultAttribute (Inherited from Attribute.)
Public methodMatch (Inherited from Attribute.)
Public methodToString (Inherited from Object.)
Top

The EntityPropertyMappingAttribute is used to define custom feed mapping in the data model of a reflection provider. This attribute is also applied to generated client data service classes when the metadata used to generate the classes indicates that custom feed mappings are defined in the data model. This information is required to make sure that the client can create and consume messages that support custom feeds. For more information, see Atom Feed Customization (WCF Data Services).

In the following example, both properties of the Order type are mapped to existing feed elements. The Product property of the Item type is mapped to a custom feed attribute in a separate namespace.

using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;

namespace CustomDataService
{
    [EntityPropertyMappingAttribute("Customer", 
        SyndicationItemProperty.AuthorName,
        SyndicationTextContentKind.Plaintext, true)]
    [EntityPropertyMapping("OrderId", 
        SyndicationItemProperty.Title, 
        SyndicationTextContentKind.Plaintext, false)]
    [DataServiceKeyAttribute("OrderId")]
    public class Order
    {
        public int OrderId { get; set; }
        public string Customer { get; set; }
        public IList<Item> Items { get; set; }
    }
    [EntityPropertyMappingAttribute("Product",
       SyndicationItemProperty.CategoryTerm,
       true, SyndicationCriteria.CategoryScheme,
       "http://northwindtraders.com/Products")]
    [EntityPropertyMappingAttribute("Url",
       SyndicationItemProperty.LinkHref, false,
       SyndicationCriteria.LinkRel,
       "http://northwindtraders.com/ProductUrl")]
    [DataServiceKeyAttribute("Product")]
    public class Item
    {
        public string Product { get; set; }
        public int Quantity { get; set; }
        public string Url { get; set; }
    }
    public partial class OrderItemData
    {
        #region Populate Service Data
        static IList<Order> _orders;
        static IList<Item> _items;
        static OrderItemData()
        {
            _orders = new Order[]{
              new Order(){ OrderId=0, Customer = "Peter Franken", Items = new List<Item>()},
              new Order(){ OrderId=1, Customer = "Ana Trujillo", Items = new List<Item>()}};
            _items = new Item[]{
              new Item(){ Product="Chai", Quantity=10, 
                  Url="http://northwindtraders.com/Teas" },
              new Item(){ Product="Chang", Quantity=25, 
                  Url="http://northwindtraders.com/Specialty"},
              new Item(){ Product="Aniseed Syrup", Quantity = 5, 
                  Url="http://northwindtraders.com/Specialty"},
              new Item(){ Product="Chef Anton's Cajun Seasoning", 
                  Quantity=30, Url="http://northwindtraders.com/Spices"}};
            _orders[0].Items.Add(_items[0]);
            _orders[0].Items.Add(_items[1]);
            _orders[1].Items.Add(_items[2]);
            _orders[1].Items.Add(_items[3]);
        }
        #endregion
        public IQueryable<Order> Orders
        {
            get { return _orders.AsQueryable(); }
        }
        public IQueryable<Item> Items
        {
            get { return _items.AsQueryable(); }
        }
    }

    public class OrderItems : DataService<OrderItemData>
    {
        // This method is called only once to initialize
        //service-wide policies.
        public static void InitializeService(DataServiceConfiguration
                                             config)
        {
            config.SetEntitySetAccessRule("Orders",
               EntitySetRights.AllRead |
               EntitySetRights.AllWrite);
            config.SetEntitySetAccessRule("Items",
                EntitySetRights.AllRead |
                EntitySetRights.AllWrite);
            config.DataServiceBehavior.MaxProtocolVersion = 
                DataServiceProtocolVersion.V3;
        }
    }
}

The previous example returns the following result for the URI http://myservice/OrderItems.svc/Orders(0)?$expand=Items.

<entry xml:base="http://localhost:12345/OrderItems.svc/" 
       xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
       xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
       xmlns="http://www.w3.org/2005/Atom">
  <id>http://localhost:12345/OrderItems.svc/Orders(0)</id>
  <title type="text">0</title>
  <updated>2009-07-25T21:12:30Z</updated>
  <author>
    <name>Peter Franken</name>
  </author>
  <link rel="edit" title="Order" href="Orders(0)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Items" 
        type="application/atom+xml;type=feed" title="Items" href="Orders(0)/Items">
    <m:inline>
      <feed>
        <title type="text">Items</title>
        <id>http://localhost:12345/OrderItems.svc/Orders(0)/Items</id>
        <updated>2009-07-25T21:12:30Z</updated>
        <link rel="self" title="Items" href="Orders(0)/Items" />
        <entry>
          <id>http://localhost:12345/OrderItems.svc/Items('Chai')</id>
          <title type="text" />
          <updated>2009-07-25T21:12:30Z</updated>
          <author>
            <name />
          </author>
          <link rel="edit" title="Item" href="Items('Chai')" />
          <category term="CustomDataService.Item" 
                    scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
          <content type="application/xml">
            <m:properties>
              <d:Product>Chai</d:Product>
              <d:Quantity m:type="Edm.Int32">10</d:Quantity>
            </m:properties>
          </content>
          <orders:productname 
            xmlns:orders="http://schema.examples.microsoft.com/dataservices">Chai</orders:productname>
        </entry>
        <entry>
          <id>http://localhost:12345/OrderItems.svc/Items('Chang')</id>
          <title type="text" />
          <updated>2009-07-25T21:12:30Z</updated>
          <author>
            <name />
          </author>
          <link rel="edit" title="Item" href="Items('Chang')" />
          <category term="CustomDataService.Item" 
                    scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
          <content type="application/xml">
            <m:properties>
              <d:Product>Chang</d:Product>
              <d:Quantity m:type="Edm.Int32">25</d:Quantity>
            </m:properties>
          </content>
          <orders:productname 
            xmlns:orders="http://schema.examples.microsoft.com/dataservices">Chang</orders:productname>
        </entry>
      </feed>
    </m:inline>
  </link>
  <category term="CustomDataService.Order" 
            scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Customer>Peter Franken</d:Customer>
    </m:properties>
  </content>
</entry>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft