Serialization of Entity Framework Models with .NET WCF Rest Service
- by Chris Phillips
I'm trying to put together a very simple REST-style interface for communicating with our partners. An example object in the API is a partner, which we'd like to have serialized like this:
<partner>
<id>ID</id>
<name>NAME</name>
</partner>
This is fairly simply to achieve using the .NET 4.0 WCF REST template if we simply declare a partner class as:
public class Partner
{
public int Id {get; set;}
public string Name {get; set;}
}
But when I use the Entity Framework to define and store Partner objects, the resulting serialization looks something like this:
<Partner p1:Id="NCNameString" p1:Ref="NCNameString" xmlns:p1="http://schemas.microsoft.com/2003/10/Serialization/" xmlns="http://schemas.datacontract.org/2004/07/TheTradeDesk.AdPlatform.Provisioning">
<EntityKey p1:Id="NCNameString" p1:Ref="NCNameString" xmlns="http://schemas.datacontract.org/2004/07/System.Data.Objects.DataClasses">
<EntityContainerName xmlns="http://schemas.datacontract.org/2004/07/System.Data">String content</EntityContainerName>
<EntityKeyValues xmlns="http://schemas.datacontract.org/2004/07/System.Data">
...
This XML is obviously unacceptable for use as an external API. What are suggested mechanisms for using EF for the data store but maintaining a simple XML serialization interface?