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
There is no recursive loop. The
Vehiclepoints to the originalVehicleOwner. It is just back reference and it cannot be avoided (only if you remove back referencing navigation property).