Frage

I see how to eager load multiple levels of entities with an include statement. But what if I'm loading a tree? Consider this hierarchy of entities:

  • Alpha
    • Bravo //a collection
      • Charlie //a collection
      • Delta //a collection

Now suppose I want to load Alpha entities that have all Bravo, Charlie, and Delta entities added. Can this be done with a single Include statement? I suspect the answer is no, but I'd like to be sure.

Otherwise, I suspect I would simply use two Include statements. But even that gives me pause, because it means I would have to reference Bravo twice (once per Include) statement and I don't know if that confuses EF.

War es hilfreich?

Lösung

It can be done with two Includes like this:

var entities = context.Alphas.Include("Bravos.Charlies")
                             .Include("Bravos.Deltas");

Or this:

var entities = context.Alphas.Include(a => a.Bravos.Select(b => b.Charlies))
                             .Include(a => a.Bravos.Select(b => b.Deltas));

Obviously First is more elegant.

Andere Tipps

No, it can't be done with one Include.

You can chain includes like this:

var entities = context.Alphas.Include("Bravos").Include("Bravos.Charlies").Include("Bravos.Deltas");
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top