asp.net-mvc – MVC 4,Upshot实体循环引用

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – MVC 4,Upshot实体循环引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个DbDataController,它提供了一个设备列表.
public IQueryable<BettrFit.Models.Equipment> GetEquipment() {
        var q= DbContext.EquipmentSet.OrderBy(e => e.Name);
        return q;
    }

在我的脚手架视图中,一切看起来都不错.

但是Equipment包含EquipmentType的HashSet成员.我想在我的视图中显示此类型,并且还能够将数据添加到Equipment的EquipmentType集合中(通过多选列表).

但是如果我尝试在我的linq查询中包含“EquipmentType”,它会在序列化期间失败.

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

“类型EquipmentType的对象图包含循环,如果禁用参考跟踪则无法序列化”

如何打开“回溯引用”?

也许问题是EquipmentType是通过HashSet反向链接的?但我在查询中没有.include(“EquipmentType.Equipment”).所以那应该没问题.

Upshot如何生成模型?我只找到了Equipmentviewmodel.js文件,但这不包含任何模型成员.

这是我的模型类:

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; }
}

解决方法

尝试使用[IgnoreDataMember]装饰其中一个导航属性
[IgnoreDataMember]
public virtual ICollection<Equipment> Equipment { get; set; }

猜你在找的asp.Net相关文章