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. 

Inheritance Hierarchy

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

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple := True, Inherited := True)> _
Public NotInheritable Class EntityPropertyMappingAttribute _
    Inherits Attribute
'Usage
Dim instance As EntityPropertyMappingAttribute
[AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public sealed class EntityPropertyMappingAttribute : Attribute
[AttributeUsageAttribute(AttributeTargets::Class, AllowMultiple = true, Inherited = true)]
public ref class EntityPropertyMappingAttribute sealed : public Attribute
[<SealedAttribute>]
[<AttributeUsageAttribute(AttributeTargets.Class, AllowMultiple = true, Inherited = true)>]
type EntityPropertyMappingAttribute =  
    class 
        inherit Attribute 
    end
public final class EntityPropertyMappingAttribute extends Attribute

The EntityPropertyMappingAttribute type exposes the following members.

Constructors

  Name Description
Public method EntityPropertyMappingAttribute(String, SyndicationItemProperty, SyndicationTextContentKind, Boolean) Creates a new instance of the EntityPropertyMappingAttribute.
Public method EntityPropertyMappingAttribute(String, String, String, String, Boolean) Creates an instance of the EntityPropertyMappingAttribute to map a property to a custom feed element.

Top

Properties

  Name Description
Public property KeepInContent Gets 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 property SourcePath Gets the name of the property of the syndication item that will be mapped to the specified element of the feed.
Public property TargetNamespacePrefix Gets a string value that, together with TargetNamespaceUri, specifies the namespace in which the TargetPathelement exists.
Public property TargetNamespaceUri Gets a string value that specifies the namespace URI of the element specified by the TargetPath property.
Public property TargetPath Gets the name of the custom target in the feed to which the property is mapped.
Public property TargetSyndicationItem Gets the syndication item in the entry targeted by the mapping.
Public property TargetTextContentKind Gets the type of content of the property mapped by EntityPropertyMappingAttribute.
Public property TypeId (Inherited from Attribute.)

Top

Methods

  Name Description
Public method Equals (Inherited from Attribute.)
Public method GetHashCode (Inherited from Attribute.)
Public method GetType (Inherited from Object.)
Public method IsDefaultAttribute (Inherited from Attribute.)
Public method Match (Inherited from Attribute.)
Public method ToString (Inherited from Object.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method _Attribute.GetIDsOfNames (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfo (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfoCount (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.Invoke (Inherited from Attribute.)

Top

Remarks

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 Feed Customization (WCF Data Services).

Examples

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.

Imports System
Imports System.Collections.Generic
Imports System.Data.Services
Imports System.Data.Services.Common
Imports System.Linq

Namespace CustomDataService
    <EntityPropertyMappingAttribute("Customer", _
        SyndicationItemProperty.AuthorName, _
        SyndicationTextContentKind.Plaintext, True)> _
    <EntityPropertyMapping("OrderId", _
        SyndicationItemProperty.Title, _
        SyndicationTextContentKind.Plaintext, False)> _
    <DataServiceKeyAttribute("OrderId")> _
    Public Class Order
        Private _orderId As Integer
        Private _customer As String
        Private _items As IList(Of Item)
        Public Property OrderId() As Integer
            Get
                Return _orderId
            End Get
            Set(ByVal value As Integer)
                _orderId = value
            End Set
        End Property
        Public Property Customer() As String
            Get
                Return _customer
            End Get
            Set(ByVal value As String)
                _customer = value
            End Set
        End Property
        Public Property Items() As IList(Of Item)
            Get
                Return _items
            End Get
            Set(ByVal value As IList(Of Item))
                _items = value
            End Set
        End Property
    End Class
    <EntityPropertyMappingAttribute("Product", _
       SyndicationItemProperty.CategoryTerm, _
       true, SyndicationCriteria.CategoryScheme, _
       "https://northwindtraders.com/Products")> _ 
    <EntityPropertyMappingAttribute("Url", _
       SyndicationItemProperty.LinkHref, false, _
       SyndicationCriteria.LinkRel, _
       "https://northwindtraders.com/ProductUrl")> 
    <DataServiceKeyAttribute("Product")> _
    Public Class Item
        Private _product As String
        Private _quantity As Integer
        Private _url As String
        Public Property Product() As String
            Get
                Return _product
            End Get
            Set(ByVal value As String)
                _product = value
            End Set
        End Property
        Public Property Quantity() As Integer
            Get
                Return _quantity
            End Get
            Set(ByVal value As Integer)
                _quantity = value
            End Set
        End Property
        Public Property Url As String
            Get
                Return _url
            End Get
            Set(ByVal value As String)
                _url = value
            End Set
        End Property
    End Class
    Partial Public Class OrderItemData
#Region "Populate Service Data"
        Shared _orders As IList(Of Order)
        Shared _items As IList(Of Item)
        Sub New()
            _orders = New Order() { _
                New Order() With {.OrderId = 0, .Customer = "Peter Franken", .Items = New List(Of Item)()}, _
              New Order() With {.OrderId = 1, .Customer = "Ana Trujillo", .Items = New List(Of Item)()}}
            _items = New Item() { _
              New Item() With {.Product = "Chai", .Quantity = 10, _
                  .Url = "https://northwindtraders.com/Teas"}, _
              New Item() With {.Product = "Chang", .Quantity = 25, _
                  .Url = "https://northwindtraders.com/Specialty"}, _
              New Item() With {.Product = "Aniseed Syrup", .Quantity = 5, _
                  .Url = "https://northwindtraders.com/Specialty"}, _
              New Item() With {.Product = "Chef Anton's Cajun Seasoning", _
                               .Quantity = 30, .Url = "https://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))
        End Sub
#End Region
        Public ReadOnly Property Orders() As IQueryable(Of Order)
            Get
                Return _orders.AsQueryable()
            End Get
        End Property
        Public ReadOnly Property Items() As IQueryable(Of Item)
            Get
                Return _items.AsQueryable()
            End Get
        End Property
    End Class
    Public Class OrderItems
        Inherits DataService(Of OrderItemData)
        ' This method is called only once to initialize
        ' service-wide policies.
        Shared Sub InitializeService(ByVal config As DataServiceConfiguration)
            config.SetEntitySetAccessRule("Orders", _
                                          EntitySetRights.AllRead _
                                          Or EntitySetRights.AllWrite)
            config.SetEntitySetAccessRule("Items", _
                                          EntitySetRights.AllRead _
                                          Or EntitySetRights.AllWrite)
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3
        End Sub
    End Class
End 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,
       "https://northwindtraders.com/Products")]
    [EntityPropertyMappingAttribute("Url",
       SyndicationItemProperty.LinkHref, false,
       SyndicationCriteria.LinkRel,
       "https://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="https://northwindtraders.com/Teas" },
              new Item(){ Product="Chang", Quantity=25, 
                  Url="https://northwindtraders.com/Specialty"},
              new Item(){ Product="Aniseed Syrup", Quantity = 5, 
                  Url="https://northwindtraders.com/Specialty"},
              new Item(){ Product="Chef Anton's Cajun Seasoning", 
                  Quantity=30, Url="https://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 https://myservice/OrderItems.svc/Orders(0)?$expand=Items.

<entry xml:base="https://localhost:12345/OrderItems.svc/" 
       xmlns:d="https://schemas.microsoft.com/ado/2007/08/dataservices" 
       xmlns:m="https://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
       xmlns="https://www.w3.org/2005/Atom">
  <id>https://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="https://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>https://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>https://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="https://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="https://schema.examples.microsoft.com/dataservices">Chai</orders:productname>
        </entry>
        <entry>
          <id>https://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="https://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="https://schema.examples.microsoft.com/dataservices">Chang</orders:productname>
        </entry>
      </feed>
    </m:inline>
  </link>
  <category term="CustomDataService.Order" 
            scheme="https://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Customer>Peter Franken</d:Customer>
    </m:properties>
  </content>
</entry>

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

System.Data.Services.Common Namespace

Other Resources

How to: Create a Data Service Using the Reflection Provider (WCF Data Services)