WCF 数据服务 使您可以在数据服务响应中自定义 Atom 序列化,以便可以将实体属性映射到在 AtomPub 协议中定义的未使用的元素。 本主题演示如何使用实体框架提供程序为 .edmx 文件中定义的数据模型中的实体类型定义映射特性。 有关更多信息,请参见 源自定义(WCF 数据服务)。
在本主题中,您将手动修改由工具生成且包含数据模型的 .edmx 文件。 由于实体设计器不支持数据模型扩展,因此必须手动修改此文件。 有关实体数据模型工具生成的 .edmx 文件的更多信息,请参见 .edmx File Overview (Entity Framework)。 本主题中的示例使用 Northwind 示例数据服务和自动生成的客户端数据服务类。 此服务和这些客户端数据类是在完成 WCF 数据服务快速入门时创建的。
手动修改 Northwind.edmx 文件以添加源自定义特性
在**“解决方案资源管理器”中右击 Northwind.edmx 文件,然后单击“打开方式”**。
在**“打开方式 - Northwind.edmx”对话框中选择“XML 编辑器”,然后单击“确定”**。
找到 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>
保存更改并关闭 Northwind.edmx 文件。
(可选)右击 Northwind.edmx 文件,并单击**“运行自定义工具”**。
此时将重新生成可能需要的对象层文件。
重新编译该项目。
示例
以上示例为 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>