加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-15 23:53:25 所属栏目:asp.Net 来源:网络整理
导读:我有一个DbDataController,它提供了一个设备列表. public IQueryableBettrFit.Models.Equipment GetEquipment() { var q= DbContext.EquipmentSet.OrderBy(e = e.Name); return q; } 在我的脚手架视图中,一切看起来都不错. 但是Equipment包含EquipmentType的
我有一个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; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读