How to: Customize Feeds with the Entity Framework Provider (WCF Data Services)

WCF Data Services enables you to customize the Atom serialization in a data service response so that properties of an entity may be mapped to unused elements that are defined in the AtomPub protocol. This topic shows how to define mapping attributes for the entity types in a data model that is defined in an .edmx file by using the Entity Framework provider. For more information, see Feed Customization.

In this topic you will manually modify the tool-generated .edmx file that contains the data model. You must manually modify the file because extensions to the data model are not supported by the Entity Designer. For more information about the .edmx file that the Entity Data Model tools generate, see .edmx File Overview (Entity Framework). The example in this topic uses the Northwind sample data service and autogenerated client data service classes. This service and the client data classes are created when you complete the WCF Data Services quickstart.

To manually modify the Northwind.edmx file to add feed customization attributes

  1. In Solution Explorer, right-click the Northwind.edmx file, and then click Open with.

  2. In the Open With - Northwind.edmx dialog box, select XML Editor, and then click OK.

  3. Locate the ConceptualModels element and replace the existing Customers entity type with the following element that contains feed customization mapping attributes:

    <EntityType Name="Customers"
        <PropertyRef Name="CustomerID" />
      <Property Name="CustomerID" Type="String" Nullable="false"
               MaxLength="5" Unicode="true" FixedLength="true" />
      <Property Name="ContactName" Type="String" MaxLength="30" 
                Unicode="true" FixedLength="false"
      <Property Name="CompanyName" Type="String" Nullable="false"
                MaxLength="40" Unicode="true" FixedLength="false"
      <Property Name="ContactTitle" Type="String" MaxLength="30" 
                Unicode="true" FixedLength="false" />
      <Property Name="Address" Type="String" MaxLength="60" 
                Unicode="true" FixedLength="false" />
      <Property Name="City" Type="String" MaxLength="15" 
                Unicode="true" FixedLength="false" />
      <Property Name="Region" Type="String" MaxLength="15" 
                Unicode="true" FixedLength="false" />
      <Property Name="PostalCode" Type="String" MaxLength="10" 
                Unicode="true" FixedLength="false" />
      <Property Name="Country" Type="String" MaxLength="15" 
                Unicode="true" FixedLength="false" />
      <Property Name="Phone" Type="String" MaxLength="24" 
                Unicode="true" FixedLength="false" />
      <Property Name="Fax" Type="String" MaxLength="24" 
                Unicode="true" FixedLength="false" />
      <NavigationProperty Name="Orders" 
                          FromRole="Customers" ToRole="Orders" />
  4. Save changes and close the Northwind.edmx file.

  5. (Optional) Right-click the Northwind.edmx file and then click Run Custom Tool.

    This regenerates the object layer file, which may be required.

  6. Recompile the project.


The previous example returns the following result for the URI http://myservice/Northwind.svc/Customers('ALFKI').

<entry xml:base="http://localhost:12345/Northwind.svc/" 
  <title type="text">ALFKI</title>
    <name>Peter Franken</name>
  <link rel="edit" title="Customers" href="Customers('ALFKI')" />
  <link rel="" 
        type="application/atom+xml;type=feed" title="Orders" 
        href="Customers('ALFKI')/Orders" />
  <category term="NorthwindModel.Customers" 
            scheme="" />
  <content type="application/xml">
      <d:ContactName>Peter Franken</d:ContactName>
      <d:CompanyName>Alfreds Futterkiste</d:CompanyName>
      <d:ContactTitle>Marketing Manager</d:ContactTitle>
      <d:Address>Obere Str. 57</d:Address>
      <d:Region m:null="true" />
    xmlns:Northwind="">Alfreds Futterkiste</Northwind:CompanyName>

See also