Sunday 15 April 2012

c# - NullReferenceException in Entity Framework from TryGetCachedRelatedEnd -


I have an institution that links to many organizations around it. Those entities simply link back to the main unit. I manually "Load ()" early on set attainable every unit (reference to'm open to a static class) and I turn on lazy loading and try to stop and both results at the same exception Are there.

If I look at the main unit on the runtime (unit), then one or more related records work well in any related institution. Where does a related unit (i.e. calculation = 0) have no related records, raises a NullReferenceException? Here is the stack trace for the exception:

  System.Data.Entity.Core.Objects.DataClasses.RelationshipManager.TryGetCachedRelatedEnd (string relationshipName, string targetRoleName, RelatedEnd & amp; relatedEnd) on  

It's done, that's the whole stack trace

Here's what I mean to show you a screenshot of the data:. Here enter image description work "addresses" because it is a related record is working "employee" because It has a related record because there is no such record "employment" does not work, but should say calculation = 0 no null reference exception?

FYI to "ActiveHours" and "ActiveEmployments" both "jobs" are close to work and connect to the unit through a partial class

The code that throws an exception: .

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

When I make it breakpoint, Employment NullReferenceException -. So it (as shown in the stack trace) Microsoft's unit in the framework code

I think I finally found out what was the problem, although error message for it was completely misleading (although now I know why it may not be more descriptive as well) I have answered this answer here Has kept it, if someone else comes across it and There are problems like me. In fact, the problem is multi-threading. I know what people are going to say; The unit does not support the framework of multi-threading, which I already knew, but I thought that it is lazy loading and you must add the collection as a thread. So I loaded all the data eager to preload. If I went through all the data without multi-threading, then all values ​​worked, no errors Even when I did a quick search about properties with multi-threading, even when it was doing a very simple and quick check (so most of my tests did not take exception) exception to the guarantee Found, I have to wrap multi-threaded search for 1000 times loop so that I can get an error message. After all, how did this issue get? In spite of being really eager to load all the data, the internal collections / lists that use the structure of the organization should also not secure the thread. Which makes me feel weird if they are already populated (no attachment or removal).

In summary:
EF lazy = Thread is not secure (understandable)
EF curious loaded = Thread is not secure (true, but who knows why?) Anyway, I hope that this can help someone else if they come in this situation.


No comments:

Post a Comment