MVC 4,结果实体循环引用 [英] MVC 4, Upshot entities cyclic references
问题描述
我有一个DbDataController它提供设备的清单。
公开的IQueryable< BettrFit.Models.Equipment> GetEquipment(){
变种Q = DbContext.EquipmentSet.OrderBy(E => e.Name);
返回q;
}
在我搭建的观点一切看起来正常。
但设备包含EquipmentType的HashSet的成员。我想在我看来,以显示这种类型的,也可以(通过多选列表)将数据添加到设备的EquipmentType集合。
但是,如果我尝试包括EquipmentType在我的LINQ查询的它的序列化过程中失败
公开的IQueryable< BettrFit.Models.Equipment> GetEquipment(){
变种Q = DbContext.EquipmentSet.Include(EquipmentType)排序依据(E => e.Name)。
返回q;
}
对象图的类型EquipmentType包含周期和不能被序列化,如果参考禁止追踪
如何切换在引用回溯?
也许问题在于EquipmentType是通过HashSet的背面联?但我不我的查询.INCLUDE(EquipmentType.Equipment)。所以这应该没问题。
如何结果生成模式?我只找到EquipmentViewModel.js文件,但这个不包含任何模型成员。
下面是我的模型类:
公共类设备
{
公共设备()
{
this.Exercise =新的HashSet<运动与GT;();
this.EquipmentType =新的HashSet< EquipmentType>();
this.UserDetails =新的HashSet<&的UserDetails GT;();
} 公众诠释ID {搞定;组; }
公共字符串名称{;组; }
公共字符串描述{搞定;组; }
公共字符串图片{搞定;组; }
公共字符串链接{搞定;组; }
公共字符串生产者{搞定;组; }
公共字符串视频{搞定;组; } 公共虚拟的ICollection< EquipmentType> EquipmentType {搞定;组; }
公共虚拟的ICollection<&的UserDetails GT; {的UserDetails获得;组; }
}
公共类EquipmentType
{
公共EquipmentType()
{
this.Equipment =新的HashSet<设备及GT;();
this.UserDetails =新的HashSet<&的UserDetails GT;();
} 公众诠释ID {搞定;组; }
公共字符串名称{;组; }
公共字符串描述{搞定;组; } 公共虚拟的ICollection<设备>设备{搞定;组; }
公共虚拟的ICollection<&的UserDetails GT; {的UserDetails获得;组; }
}
我想通了 - 部分如何解决循环引用问题
我只是迭代查询我的收藏(与包括()),并设置反向引用父为NULL。这工作了,否则就破坏了服务器上的序列化问题。
唯一的问题是现在数据实体的更新 - 它的失败,因为引用entitycollection的数组都是静态的...
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; }
}
I figured out - partially how to solve the circular reference problem.
I just iterated over my queried collection (with Include() ) and set the backreferences to the parent to NULL. That worked for the serialisation issue which otherwise already breaks on the server.
The only problem now is the update of a data entity - its failing because the arrays of the referenced entitycollection are static...
这篇关于MVC 4,结果实体循环引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!