Serialization of Entity Framework Models with .NET WCF Rest Service

Posted by Chris Phillips on Stack Overflow See other posts from Stack Overflow or by Chris Phillips
Published on 2010-04-26T15:18:33Z Indexed on 2010/04/26 15:43 UTC
Read the original article Hit count: 220

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET