I have created several POCO entities that have relationships between them. For instance, the "Individual" entity has a OneToMany relationship with a "Collection" entity. Here is how I defined them :
[DataContract(IsReference=true)]
[KnownType(typeof(User))]
[KnownType(typeof(Address))]
[KnownType(typeof(Collection))]
[KnownType(typeof(Donation))]
[KnownType(typeof(CollectorRating))]
[KnownType(typeof(DonatorRating))]
[Table("Individuals")]
public class Individual : User
{
// ... Lots of attributes
[DataMember]
[InverseProperty("Collector")]
public virtual ICollection<Collection> Collections { get; set; }
// Lots of other attributes
}
And the Collection entity :
[DataContract(IsReference=true)]
[KnownType(typeof(Individual))]
[KnownType(typeof(Organization))]
[KnownType(typeof(Donation))]
[KnownType(typeof(DeliveryDay))]
[KnownType(typeof(Address))]
[Table("Collections")]
public class Collection
{
// Other attributes
[DataMember]
[InverseProperty("Collections")]
public virtual Individual Collector { get; set; }
// ... Attributes
}
My service is a Silverlight compatible service, which is defined this way :
[ServiceContract(Namespace = "")]
[SilverlightFaultBehavior]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class IndividualService
{
[OperationContract]
public User GetByEmail(string email)
{
using (var context = new EntitiesContext())
{
Individual user = context.Individuals.SingleOrDefault(u => u.Email == email);
return user;
}
}
}
This works as expected : I receive an individual object which is populated with data members, and null array of collections.
However, once I try to include the relationship :
context.Individuals.Include("Collections").SingleOrDefault(u => u.Email == email)
I have a stack overflow exception, which is quite annoying. I'm pretty sure this is a cyclic reference error, however I tried every solutions (adding IsReference=true to DataContract attribute...). This only thing that works is replacing DataMember attribute by IgnoreDataMember attribute in the Collection entity, however I lose the bidirectional relationship, that is something I want for this particular entity...