asp.net-mvcrazorknockout.jsupshot

MVC 4, Upshot entities cyclic references


I have a DbDataController which delivers a List of Equipment.

    public IQueryable<BettrFit.Models.Equipment> GetEquipment() {
        var q= DbContext.EquipmentSet.OrderBy(e => e.Name);
        return q;
    }

In my scaffolded view everything looks ok.

But the Equipment contains a HashSet member of EquipmentType. I want to show this type in my view and also be able to add data to the EquipmentType collection of Equipment (via a multiselect list).

But if I try to include the "EquipmentType" in my linq query it fails during serialisation.

    public IQueryable<BettrFit.Models.Equipment> GetEquipment() {
        var q= DbContext.EquipmentSet.Include("EquipmentType").OrderBy(e => e.Name);
        return q;
    }

"Object Graph for Type EquipmentType Contains Cycles and Cannot be Serialized if Reference Tracking is Disabled"

How can I switch on the "backtracking of references"?

Maybe the problem is that the EquipmentType is back-linking through a HashSet? But I do not .include("EquipmentType.Equipment") in my query. So that should be ok.

How is Upshot generating the model? I only find the EquipmentViewModel.js file but this does not contain any model members.

Here are my model classes:

public class Equipment
{
    public Equipment()
    {
        this.Exercise = new HashSet<Exercise>();
        this.EquipmentType = new HashSet<EquipmentType>();
        this.UserDetails = new HashSet<UserDetails>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Picture { get; set; }
    public string Link { get; set; }
    public string Producer { get; set; }
    public string Video { get; set; }

    public virtual ICollection<EquipmentType> EquipmentType { get; set; }
    public virtual ICollection<UserDetails> UserDetails { get; set; }
}
public class EquipmentType
{
    public EquipmentType()
    {
        this.Equipment = new HashSet<Equipment>();
        this.UserDetails = new HashSet<UserDetails>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Equipment> Equipment { get; set; }
    public virtual ICollection<UserDetails> UserDetails { get; set; }
}

Solution

  • I figured out - partially how to solve the circular reference problem.

    I just iterated over my queried collection (with Include() ) and set the backreferences to the parent to NULL. That worked for the serialisation issue which otherwise already breaks on the server.

    The only problem now is the update of a data entity - its failing because the arrays of the referenced entitycollection are static...