EF 4.1 POCO 中递归加载中的 stackoverflow
我有一个包含 Vehicle 和 VehicleOwner 的 EF 4.1 POCO 类。
当我加载急切加载包含车辆的VehicleOwner时,车辆仍然加载VehicleOwner,当您使用Mapper时,它最终会出现stackoverflow异常。
这是代码示例
public class VehicleOwner : IVehicleOwner
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity),ScaffoldColumn(false)]
public int VehicleOwnerId { get; set; }
[Required,DisplayName("First Name")]
public string FirstName{ get; set; }
[Required,DisplayName("Last Name")]
public string LastName { get; set; }
public virtual ICollection<Vehicle> Vehicles { get; set; }
}
public class Vehicle
{
public Vehicle()
{
this.RecoveredVehicles = new HashSet<RecoveredVehicles>();
}
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VehicleId { get; set; }
[DisplayName("Identification Number")]
public string IdentificationNumber { get; set; }
[Required]
[DisplayName("Owner ID")]
public int VehicleOwnerId { get; set; }
[Required]
[ScaffoldColumn(false)]
public byte[] RowVersion { get; set; }
public virtual VehicleOwner VehicleOwner { get; set; }
public virtual ICollection<RecoveredVehicle> RecoveredVehicles { get; set; }
}
//当我这样做时,车辆会被填充,当您展开车辆时,VehicleOwner 会再次被填充。
public VehicleOwner GetVehicleOwner(int vehicleOwnerID)
{
objDataContext.Configuration.ProxyCreationEnabled = false;
return (from p in objDataContext.VehicleOwners.Include("Vehicles") where p.VehicleOwnerId == vehicleOwnerID select p).FirstOrDefault<VehicleOwner>();
}
有没有办法可以防止这种递归循环。
谢谢
I have an EF 4.1 POCO class with Vehicle and VehicleOwner.
When i load eager load the VehicleOwner including the Vehicles, the Vehicles still load the VehicleOwner and when you with a Mapper it ends up in a stackoverflow Exception.
Here is the code sample
public class VehicleOwner : IVehicleOwner
{
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity),ScaffoldColumn(false)]
public int VehicleOwnerId { get; set; }
[Required,DisplayName("First Name")]
public string FirstName{ get; set; }
[Required,DisplayName("Last Name")]
public string LastName { get; set; }
public virtual ICollection<Vehicle> Vehicles { get; set; }
}
public class Vehicle
{
public Vehicle()
{
this.RecoveredVehicles = new HashSet<RecoveredVehicles>();
}
[Key,DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VehicleId { get; set; }
[DisplayName("Identification Number")]
public string IdentificationNumber { get; set; }
[Required]
[DisplayName("Owner ID")]
public int VehicleOwnerId { get; set; }
[Required]
[ScaffoldColumn(false)]
public byte[] RowVersion { get; set; }
public virtual VehicleOwner VehicleOwner { get; set; }
public virtual ICollection<RecoveredVehicle> RecoveredVehicles { get; set; }
}
//when i do this the vehicles get populated and when you expand the Vehicles the VehicleOwner is populated again.
public VehicleOwner GetVehicleOwner(int vehicleOwnerID)
{
objDataContext.Configuration.ProxyCreationEnabled = false;
return (from p in objDataContext.VehicleOwners.Include("Vehicles") where p.VehicleOwnerId == vehicleOwnerID select p).FirstOrDefault<VehicleOwner>();
}
Is there a way i can prevent this recursive loop.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不存在递归循环。
Vehicle
指向原始的VehicleOwner
。它只是反向引用,无法避免(仅当您删除反向引用导航属性时)。There is no recursive loop. The
Vehicle
points to the originalVehicleOwner
. It is just back reference and it cannot be avoided (only if you remove back referencing navigation property).