Compartilhar via


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

Important

WCF Data Services has been deprecated and will no longer be available for download from the Microsoft Download Center. WCF Data Services supported earlier versions of the Microsoft OData (V1-V3) protocol only and has not been under active development. OData V1-V3 has been superseded by OData V4, which is an industry standard published by OASIS and ratified by ISO. OData V4 is supported through the OData V4 compliant core libraries available at Microsoft.OData.Core. Support documentation is available at OData.Net, and the OData V4 service libraries are available at Microsoft.AspNetCore.OData.

RESTier is the successor to WCF Data Services. RESTier helps you bootstrap a standardized, queryable, HTTP-based REST interface in minutes. Like WCF Data Services before it, Restier provides simple and straightforward ways to shape queries and intercept submissions before and after they hit the database. And like Web API + OData, you still have the flexibility to add your own custom queries and actions with techniques you're already familiar with.

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"
                m:FC_SourcePath="CustomerID"
                m:FC_TargetPath="SyndicationTitle"
                m:FC_ContentKind="text"
                m:FC_KeepInContent="false"
                >
       <Key>
        <PropertyRef Name="CustomerID" />
      </Key>
      <Property Name="CustomerID" Type="String" Nullable="false"
               MaxLength="5" Unicode="true" FixedLength="true" />
      <Property Name="ContactName" Type="String" MaxLength="30" 
                Unicode="true" FixedLength="false"
                m:FC_TargetPath="SyndicationAuthorName"
                m:FC_ContentKind="text"
                m:FC_KeepInContent="true"  
                />
      <Property Name="CompanyName" Type="String" Nullable="false"
                MaxLength="40" Unicode="true" FixedLength="false"
                m:FC_TargetPath="CompanyName"
                m:FC_NsPrefix="Northwind"
                m:FC_NsUri="http://schemas.examples.microsoft.com/dataservices"
                m:FC_KeepInContent="true"
                />
      <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" 
                          Relationship="NorthwindModel.FK_Orders_Customers" 
                          FromRole="Customers" ToRole="Orders" />
    </EntityType>
    
  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.

Example

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

<entry xml:base="http://localhost:12345/Northwind.svc/" 
       xmlns:d="https://schemas.microsoft.com/ado/2007/08/dataservices" 
       xmlns:m="https://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
       xmlns="http://www.w3.org/2005/Atom">
  <id>http://localhost:12345/Northwind.svc/Customers('ALFKI')</id>
  <title type="text">ALFKI</title>
  <updated>2009-07-27T07:59:43Z</updated>
  <author>
    <name>Peter Franken</name>
  </author>
  <link rel="edit" title="Customers" href="Customers('ALFKI')" />
  <link rel="https://schemas.microsoft.com/ado/2007/08/dataservices/related/Orders" 
        type="application/atom+xml;type=feed" title="Orders" 
        href="Customers('ALFKI')/Orders" />
  <category term="NorthwindModel.Customers" 
            scheme="https://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <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:City>Berlin</d:City>
      <d:Region m:null="true" />
      <d:PostalCode>12209</d:PostalCode>
      <d:Country>Germany</d:Country>
      <d:Phone>089-0877310</d:Phone>
      <d:Fax>089-0877554</d:Fax>
    </m:properties>
  </content>
  <Northwind:CompanyName 
    xmlns:Northwind="http://schemas.examples.microsoft.com/dataservices">Alfreds Futterkiste</Northwind:CompanyName>
</entry>

See also