A simple solution to a complicated error for anyone to try out before searching any further : Having to write a WCF service to handle complex LINQ entities with various foreign key references I came across the following exception when trying to consume the respective service :
Object graph for type “EntityType” contains cycles and cannot be serialized if reference tracking is disabled.’
Trying to find out what was wrong I came across various solutions in Internet forums , one more complicated as the other. Bearing in mind that it couldn’t be that bad I tried to eliminate code parts to test how far back it goes. This way I came to the problem : in my client application where I set the object properties I was referencing the parent object (table) twice : once setting the complete parent object in the child object reference and secondly by adding the child to the list of children of the parent object, already having the reference to parent set this way. So my problem was solved by only adding the children objects in the list within the parent and everything else was taken care of while inserted in the database.
Hi, there is a better and easier way.
You should decorate the DTO or the Linq Entity in this way:
[DataContract(Name = "MyClass", Namespace = "Dto", IsReference = true)]
public sealed class MyClass
{
}
You have to do the same for Parent and Child objects.
Cheers, Raffaeu.