2

I have an entity that links to lots of entities around it. Those entities only link back to the main entity. I'm manually "Load()"ing every entity set on the context at startup (the context remains open in a static class) and I've tried this with lazy loading on and off and both result in the same exception.

If I look at the core entity at run time (Person entity), any related entities that have one or more related records work fine. Any where there are no related records for the related entity (i.e. count = 0) raises a NullReferenceException?! Here is the stack trace for the exception:

   at System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.TryGetCachedRelatedEnd(String relationshipName, String targetRoleName, RelatedEnd& relatedEnd)

That's it, that's the whole stack trace.

Here is a screenshot of the data to show you what I mean: enter image description here "Addresses" works because it has a related record. "Employees" works because it has a related record. "Employments" does not work because there are no records but it should say Count = 0 not null reference exception?!

FYI "ActiveHours" and "ActiveEmployments" both work off "Employments" and are added to the entity via a partial class.

The code that throws the exception is:

this.Employments.Where(employment => employment.IsActive);

When I breakpoint it, Employments creates the NullReferenceException - so it is in Microsoft's Entity Framework code (as shown in the stack trace).

Anupheaus
  • 3,383
  • 2
  • 24
  • 30
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Mar 18 '15 at 02:21
  • What's the code of `Employments` and what code throws this exception? – Gert Arnold Mar 18 '15 at 07:37
  • @GertArnold have updated post to show code that throws the exception. – Anupheaus Mar 18 '15 at 09:21
  • @PeterDuniho I know what a NullReferenceException is and how to solve it, but I can't solve it if I don't own or cannot see the code or cannot find a logical reason for the exception can I? Hence the reason I've put it on SO. – Anupheaus Mar 18 '15 at 09:23

1 Answers1

3

I think I finally figured out what the problem was, though the error message for it was completely misleading (though I do now know why it couldn't be more descriptive as well). I've put this answer here in case someone else comes across it and has the same problems as I did. Basically, the problem is multi-threading. I know what people are going to say; entity framework does not support multi-threading, which I already knew, but I thought that was because of lazy loading and adding to the collection as a thread needed it. So I used eager loading to pre-load all of the data. If I went through all of the data without multi-threading, all of the values worked, no errors. Even when I did a quick search through the properties with multi-threading, most of the time it would work, when just doing a very simple and quick check (hence the reason most of my tests didn't raise the exception). In order to guarantee I got the exception, I'd have to wrap the multi-threaded search in a for loop of 1000 times to ensure I got the error message. This is how I finally found the issue. Despite actually eager loading all of the data, the internal collections/lists that entity framework uses must also not be thread safe. Which I find quite strange if they are already populated (no appends or removes).

In summary:
EF Lazy Loaded = Not Thread Safe (Understandable)
EF Eager Loaded = Not Thread Safe (True, but who knows why?)

Anyway, I hope this might help someone else if they ever get into this situation.

Anupheaus
  • 3,383
  • 2
  • 24
  • 30