For some reason, navigation properties are not working on my Entity Framework model.
From the direction N->1, every time I attempt to obtain a EntityReference it comes with null value, even though the EntityKey is correct.
From the direction 1->N, the collection is always empty.
This behavior is consistent throughout my entire model.
Whatever the reason is, I think it should raise an exception intead of silently retrieving an inconsistent reference.
What are the possible reasons a reference would come with null value?
EDIT
I just noticed it has something to do with lazy loading. The EntityReference(T) is coming with the IsLoaded property set to false and the explicit call of the Load method solves the problem. The problem is that the method RelationshipManager.GetRelatedReference that is called when the navigation property is accessed should load the EntityReference. Should’nt it?
This problem is because of the meaning of the
LazyLoadingEnabledproperty.At first glance,
LazyLoadingEnabled= false seems to mean that EF will load the object relationships when the object is loaded, of course with some limitations to prevent EF to load the entire database. Actually, it means that the relationships will never be implitly loaded. That is: from thedirection N->1, the returnedEntityReference(T)will have a correctEntityKeybutIsLoadedwill be false andValuewill be null. In the other hand, in thedirection 1->N, the collection will be empty,IsLoadedwill be false. Either theEntityReferenceor theEntityCollectioncan be explicitly loaded using theLoadmethod.LazyLoadingEnabled= true, in the other hand, means that it seems to mean. The associations will be loaded as they are needed.The default is false, which by the way triggered all this confusion I went through.
To prevent confusions, maybe there should be a property called
LoadingModewhich would be an enum with meaningful values. Like:None,Lazy,Eager