tags:

views:

228

answers:

1

Assume I have an entity named "A" that contains an EntityRef and an EntitySet. I'm curious as to exactly when, by default, the EntityRef and EntitySet are retrieved when "A" is retrieved from the database? It seems, from my experimentation, that the EntityRef is retrieved at the same time entity "A" is retrieved from the database and the EntitySet is retrieved when I starting iterating over the EntitySet entities. Is this correct?

Thanks - Randy

A: 

Yes you are correct about the defaults. You can customize the behavior in a few ways as well: In the model (depending on provider) you can set lazy-loading properties to fetch at the time the main object is fetched, or to wait until that particular property is requested (default).

Also, you can load one EntitySet per parent per query in a query with DataLoadOptions, example:

public class Parent
{
  public string Name { get; set; }
  public EntitySet<Child> Children { get; set; }
}
public class Child
{
  public string Name { get; set; }
  public int Age { get; set; }
}

var dlo = new DataLoadOptions();
dlo.LoadWith<Parent>(p => p.Children);
var dc = new DataContext();
dc.LoadOptions = dlo;

var parentAndChildren = from p in dc.Parents select p;

The above would perform a left join to fetch the children in a single query. You can find more examples in the DataLoadOptions MSDN Docs. There's also a similar AssociateWith for restricted loading you may want to check out.

Also, if you need to load the EntitySet earlier, you can call .Load() on it.

Nick Craver