How to determine if Navigation Property in the Entity Framework is set without loading the related records

Viewed 6903

I'm not sure about Navigational Properties in EF 4 so I would kindly ask you an explanation.

Lets imagine this scenarios:

A)

I have two Entities A and B with a relation N to N (many to many) and tree Table in my Data Base A and B and a Link Table AB with two Foreign Key.

In this scenario EF create a Navigational Property lets call it X and also a XReference.

B)

I have two Entities A and B with a relation 1 to N (one to many) and two Table in my Data Base A and B with one Foreign Key.

In this scenario EF create a Navigational Property lets call it Y but not a YReference.

Now lets take Scenario A and B and try to find out if there is any reference of A in B:

My Code for Scenario:

A):

bool isA = a.XReference.EntityKey != null;

I do not load B records (correct?)

B):

bool isA = a.B.Any(x => x.BId == AId);

I do load B records

My questions:

  • Why EF does not create a YReference and I cannot use EntityKey property in Scenario B.
  • In my Code Scenario B, Do I really do not load any records from B?
  • Do you know a better approach to run this query fast?

Thanks guys for your help, I hope I was able to make it clear :-)

2 Answers
Related