0

When using Include() in my LINQ against my model should the path parameter I supply be the name of the related objects or the name of the Navigation Property?

I ask because the same table has more than one relationship to the same other table. I note in the designer the navigation properties of related table are in the form:

RelatedTable
RelatedTable1
...
RelatedTablen

Lazy loading is off so I need to explicitly load related objects. I am only want the objects related through the Navigation Property: "RelatedTable1" I have tried supplying the Navigation Property name (i.e. "RelatedTable1") to Include, i.e.:

from row in Table.Include("RelatedTable1")
select row

and it works! Which suggests the path parameter should be the name of the Navigation Property. However I have also tried just the related Entity's name, i.e.:

from row in Table.Include("RelatedTable")
select row

and it works too! I am just asking to understand what is going on..

(LazyLoading is off and if I do not use Include() related is not fetched).

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
markmnl
  • 11,116
  • 8
  • 73
  • 109
  • The name of the navigation property. Of course it works. You've got both `RelatedTable` and `RelatedTable1` as navigation properties. But the joined fields are different. – Gert Arnold Aug 17 '12 at 23:13
  • thanks - you are right i shouldn't have been surprised is should have concluded it must be the navigation property being used. still i think this is something the documentation should state! – markmnl Aug 19 '12 at 05:25

1 Answers1

1

You can use the name of the Navigation properties, or you can chain them like this:

db.Tables.Include("RelatedTable.RelatedTable2");

Or if you prefer strongly typing, you could include as a lambda:

db.Tables.Include(x => x.RelatedTable);
Community
  • 1
  • 1
Mark Oreta
  • 10,346
  • 1
  • 33
  • 36