1

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
            }
Claudio Cauchi
  • 443
  • 4
  • 7
  • On one line you have `entity.NavigationPropertyId =`, later `entity.NavigationProperty.Id`, ie `Id` becomes a property of `NavigationProperty` in the second case. Is this a typo or the cause of your problem? – David Arno Jun 05 '15 at 08:22
  • Obviously either `x` is `null`, or `entity.NavigationProperty` is `null`. – Lasse V. Karlsen Jun 05 '15 at 08:39
  • Lasse, this is not a duplicate . I know what a NullReferenceException is. X cannot be null as it's the predicate of the lambda expression. The NavigationProperty is obviously null but i would expect it to be lazy loaded, hence my question. – Claudio Cauchi Jun 05 '15 at 11:46

1 Answers1

0

Well, many strange things

//with First, your entity could be almost anything, and it's NavigationProperty could perfectly be null.
var entity = context.Entities.First();
//now you set its foreign key value ??? this won't affect an eventual navigation property, if you don't save the entity...
entity.NavigationPropertyId = 24;

Then, in the SingleOrDefault, you use entity.NavigationProperty.Id, not entity.NavigationPropertyId => you still don't know if entity has a not null NavigationProperty : it's look like it has not => no lazy loading can be done on something which is null...

I guess it's sample code, but I would go for (of course, it would be much easier to use 24 directly in the second query, but you may wanna check if there's something existing in Entities with this value)

var entity = context.Entities.FirstOrDefault(x => x.NavigationProperty != null && x.NavigationProperty.Id == 24);

if (entity != null) {
var otherEntity = context
                    .OtherEntities
                    .SingleOrDefault(x =>//Are your sure a FirstOrDefault is not enough ?
                         (x.NavigationPropertyId == entity.NavigationProperty.Id));
}
Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122
  • This is sample code, I just wanted to prove a point as something similar happened in production. obviously it was more complex than this but this illustrates the point. Your point "now you set its foreign key value ??? this won't affect an eventual navigation property, if you don't save the entity" could be the answer I was looking for. I'll check if the entity was saved in the meantime. – Claudio Cauchi Jun 05 '15 at 11:54
  • Marked as answer because the entity had not been saved. Thanks for pointing that out. – Claudio Cauchi Jun 05 '15 at 11:57