I have this code that is causing a NullReferenceException. I would expect Lazy loading to kick in at the point of evaluating the lambda and go to the database fetching the Navigation Property (last line). I solved it by using the Id directly but I'm curious if anyone can point me to any documentation that explains what is happening here and why this does not work.
using (var context = new TestEntities())
{
var entity = context.Entities.First();
entity.NavigationPropertyId = 24; // This is a valid id, i.e. there is a record with Id 24 in the database
var otherEntity = context
.OtherEntities
.SingleOrDefault(x =>
(x.NavigationPropertyId == entity.NavigationProperty.Id)); // << This raises the NullReferenceException
}