Entity Framework doesn’t support web services + entity inheritance
Entity Framework supports entity inheritance, which is a basic requirement for O/R mappers.
When exposing a model through a service, either an ASMX WS or a WCF service, a WSDL will be created that among other things describes the structure of the entities, including the inheritance tree of the entities - This is done to allow creation of methods that return polymorphic types.
EF supports derived entities through WCF services with data contract serialization using the KnownTypeAttribute which is added to the base class for every derived class.
To allow the same thing to be done with ASMX web services (that uses the XmlSerializer) a XmlIncludeAttribute needs to be added to the base class for every derived class – something that EF doesn’t do !!!
Without XmlIncludeAttribute, the WSDL will not include information about the derived classes, making the generated proxy in the client accept only base class values, and worse – get a soap exception when a derived type is returned from the service.
The solution for this is simple, yet annoying :
Simple - create a partial class for every base class and add an XmlIncludeAttribute for each derived class. For example, if you have a Person base class and a derived Employee class then the partial class will look like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace EFSample
{
[XmlInclude(typeof(Employee))]
public partial class Person
{
}
}
Annoying – since it’s that simple, why doesn’t EF support that?