機器のリストを提供する DbDataController があります。
public IQueryable<BettrFit.Models.Equipment> GetEquipment() {
var q= DbContext.EquipmentSet.OrderBy(e => e.Name);
return q;
}
私の足場ビューでは、すべて問題ないように見えます。
ただし、Equipment には EquipmentType の HashSet メンバーが含まれています。このタイプをビューに表示し、(複数選択リストを介して) EquipmentType コレクションの Equipment にデータを追加できるようにしたいと考えています。
しかし、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; }
}