在EF 4.1 POCO中递归加载中的stackoverflow

时间:2011-08-24 12:17:02

标签: c#-4.0 entity-framework-4.1

我有一个带有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>();        
}

有没有办法可以阻止这种递归循环。

由于

1 个答案:

答案 0 :(得分:0)

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