Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
753 views
in Technique[技术] by (71.8m points)

asp.net mvc - 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; }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

try decorating one of the navigation properties with [IgnoreDataMember]

[IgnoreDataMember]
public virtual ICollection<Equipment> Equipment { get; set; } 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...