I got this error when I started to add the navigation properties to the entities. Because a master detail relation has got 2 kinds of navigation properties. The master gets the collection of childs and the child gets the master where it belongs to. JSON serialization will end up in a circular loop, since it serializes the child, then tries to serialize the parent, but this has a collection of child etc.
You can solve this many ways.
- If you do not need the parent navigation property, you can remove it.
- You could set an attribute to the property: [JsonIgnore] [IgnoreDataMember]. The problem here is when you use the generated classes, the next time you generate it will be removed.
- Set lazy loading on your context to false: db.Configuration.LazyLoadingEnabled = false;
In my case I chose the first one, since the parent did not need to know about it's children.
I know this is not the ideal situation, better to use POCO's or simple objects. But when you already have a large structure in place using these self tracking entities, it can be usefull to do this. In my case we had webservices build for .Net client applications. Now we want to reuse this architecture and consume it on IPAD and IPHONE. Can this work? Yes offcourse. First I tried with SOAP, which works well, but there were 3 problems. First I was relying on a object generator tool of SUDZC, second SOAP is taking more data then JSON, third, iOS doesn't support nillable and the generator converted it into simple types. So now I decided to use JSON, use my own objects and reuse the webservices.
This involved making a new service layer the consumes the same process layer as my .Net WCF services.
But you will notice that when you try to pass a self tracking entity in JSON format, the webservice will crash.
The solution to this was:
Set the IsReference flag to false, and remove the DataMember attribute from the ChangeTracker.
public ObjectChangeTracker ChangeTracker
[DataContract(IsReference = false)]
public partial class GP_MES_Clients: IObjectWithChangeTracker, INotifyPropertyChanged
After this all is changed, you can consume this service in your iOS application!