我有一个提供设备列表的 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; }
}