I have an ASP.Net MVC SPA using the Hot Towel template i.e. breezejs, knockout, entity framework (code first), durandal etc.
In my EF model I have a class named "Section" that has a self referencing association. Each section belongs to a "Document" and each Section also has a collection of "Items":
public class Section : CommonBase
{
...
public Guid DocumentId { get; set; }
public Document Document { get; set; }
...
public List<Item> Items { get; set; }
public Guid? ParentId { get; set; }
public Section Parent { get; set; }
public List<Section> Children { get; set; }
...
}
public class Item : CommonBase
{
...
public Guid SectionId { get; set; }
public Section Section { get; set; }
...
}
I load the "Sections" and "Items" when I load the documents via a Breeze query and a BreezeController method:
var query = breeze.EntityQuery.from(model.entitySets.document)
.where(predicate)
.expand("sections.items.cloudDriveFile, sections.cloudDriveFile")
.orderBy(model.orderByClauses.document);
return _contextProvider.Context.Documents.Where(x => x.OrganisationId == currentUser.OrganisationId);
If I edit and save a "Section" without loading any "Items" then all works OK. However when I try to edit and save a section that has "Items" - and I have loaded those "Items" (either using an Include in the BreezeController or an Expand in javascript) - then I get the following error:
"Uncaught TypeError: Converting circular structure to JSON "
I am saving using a simple call to manager.saveChanges().
Is there some technique or pattern that I should be implementing to avoid this circular reference error?