共用方式為


HOW TO:透過 Entity Framework 提供者自訂摘要 (WCF Data Services)

WCF Data Services 可讓您自訂資料服務回應中的 Atom 序列化,因此可以將實體的屬性對應至在 AtomPub 通訊協定中定義的未使用項目。 本主題說明如何使用 Entity Framework 提供者,針對資料模型中於 .edmx 檔中定義的實體類型定義對應屬性。 如需詳細資訊,請參閱 摘要自訂 (WCF Data Services)

在本主題中,您將手動修改工具產生的 .edmx 檔 (其中包含資料模型)。 您必須手動修改此檔案,因為 Entity Designer 不支援資料模型的副檔名。 如需實體資料模型工具產生之 .edmx 檔案的詳細資訊,請參閱 .edmx File Overview (Entity Framework)。 本主題的範例使用 Northwind 範例資料服務,以及自動產生的用戶端資料服務類別。 此服務和用戶端資料類別會在您完成 WCF Data Services 快速入門時建立。

手動修改 Northwind.edmx 檔以加入摘要自訂屬性

  1. 在 [方案總管] 中,以滑鼠右鍵按一下 Northwind.edmx 檔案,然後按一下 [開啟方式]。

  2. 選取 [開啟方式 - Northwind.edmx] 對話方塊中的 [XML 編輯器],然後按一下 [確定]。

  3. 找出 ConceptualModels 項目,並以下列包含摘要自訂對應屬性的項目取代現有的 Customers 實體類型:

    <EntityType Name="Customer"
                m:FC_SourcePath="CustomerID"
                m:FC_TargetPath="SyndicationTitle"
                m:FC_ContentKind="text"
                m:FC_KeepInContent="true">
      <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" 
                m:FC_TargetPath="SyndicationLinkHref"
                m:FC_ContentKind="text"
                m:FC_KeepInContent="true"
                m:FC_Criteria="SyndicationLinkRel"
                m:FC_CriteriaValue="http://NorthwindTraders.com/MyPhotos" />
      <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. 儲存變更並關閉 Northwind.edmx 檔。

  5. (選擇性) 以滑鼠右鍵按一下 Northwind.edmx 檔,然後按一下 [執行自訂工具]。

    這樣將會重新產生您可能需要的物件層檔案。

  6. 重新編譯專案。

範例

上一個範例會傳回下列 URI 結果http://myservice/ Northwind.svc/Customers('ALFKI')

<entry xml:base="https://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>https://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>

另請參閱

概念

Entity Framework 提供者 (WCF Data Services)