0

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...

Kara
  • 6,115
  • 16
  • 50
  • 57
Michael Gallego
  • 1,746
  • 1
  • 20
  • 29

1 Answers1

0

Avoid n-level recursion in your datacontracts as you will have problems with de-serializing them (stack overflow can result). Consider flattening your DTO structure. You could model the relationships between parents and children without recursion.

Alternately, try and identify the maximum level of recursion required to be supported and model it explicitly in your DTO structure.

Nick Ryan
  • 2,662
  • 1
  • 17
  • 24
  • Quite a bit. However, my models are already plain old POCO objects. Does this mean that I have to create another classes (DTO objects) that flatten the POCO structure ? I mean, this is like writing and maintaining both the same time. – Michael Gallego Mar 06 '12 at 14:04