EF 4.1 POCO 中递归加载中的 stackoverflow

发布于 2024-12-01 07:35:31 字数 1653 浏览 0 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枕花眠 2024-12-08 07:35:31

不存在递归循环。 Vehicle 指向原始的VehicleOwner。它只是反向引用,无法避免(仅当您删除反向引用导航属性时)。

There is no recursive loop. The Vehicle points to the original VehicleOwner. It is just back reference and it cannot be avoided (only if you remove back referencing navigation property).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文