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:
EntityKey
property in Scenario B.Thanks guys for your help, I hope I was able to make it clear :-)
Here is a way to check if related records for an entity is loaded or not.
For entity where you have multiple records related to entity you can check like below.(One to many relationship)
myDbContext.Entry(MyEntity).Collection(x => x.NavigationalProperyName).IsLoaded
And If have only one record related to entity, then you can check like below.(One to one relationship)
myDbContext.Entry(MyEntity).Reference(x => x.NavigationalProperyName).IsLoaded
Do you mean:
// -to-one relationship
entity.RelatedItemReference.IsLoaded
// -to-many relationship
entity.RelatedItems.IsLoaded
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With