Reputation: 1295
I again have the following two classes, that are generated by my test model:
public partial class House
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public House()
{
this.Persons = new HashSet<Person>();
}
public int id { get; set; }
public string street { get; set; }
public string city { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Person> Persons { get; set; }
}
public partial class Person
{
public int id { get; set; }
public string namen { get; set; }
public int house { get; set; }
public virtual House House1 { get; set; }
}
In the constructor of my DBContext I explicitly enable Lazy Loading, what should not even be necessary:
this.Configuration.LazyLoadingEnabled = true;
this.Configuration.ProxyCreationEnabled = true;
Later I call a list of houses:
YardEntities cx = new YardEntities();
IList<House> hl = cx.Houses.ToList<House>();
House h = hl[0];
///**************BREAKPOINT*******************///
Person g = h.Persons.First<Person>();
output = g.namen;
After the marked breakpoint I expected, that all the persons associated with the houses are not loaded at this point because of lazy loading. I did not access a single person so why should they be loaded? However they are loaded, because my debugger shows me all the name attributes.
Can somebody explain this behaviour? Why is Lazy loading not working?
Upvotes: 0
Views: 71
Reputation: 1295
If I do not use tolist():
DbSet<House> hl = cx.Houses;
House h = hl.FirstOrDefault<House>();
h contains all persons even though lazy loading is enabled. I guess that Arvins answer must be true. The debugger loads all the values when I want to see them. But is there a way to proof his guess?
Upvotes: 0
Reputation: 14995
I did not access a single person so why should they be loaded?
You are right. lazy loading won't load any relation unless you want to access them.
However they are loaded, because my debugger shows me all the name attributes.
well here actually your debugger wants to access them, so lazy loading will do the work and get them from database.
Upvotes: 3