2

Edit: This is not a problem with ignorance of basic programming (such as trying to dereference a null object reference).

Edit: Added the stack trace from EF within Linqpad.

Using EF6, I have a very simple query :

var menu = dbcontext.Tree.Where(t => t.Left > 2 && t.Right < 10).ToList();

This worked right up until it mysteriously stopped. dbcontext.Tree is a view in SQL Server 2012. Using Linqpad5, I get the results I expect using its built-in connection. Setting up an EF connection to my project, I get the NRE. Checking the SQL, I can copy and paste that into a SQL query window and get the proper results. I get an NRE without the Where call, also.

I've tried updating my model from database to refresh anything. I've tried removing the view from the model and updating. I've tried deleting the model entirely and recreating it. I've restarted Visual Studio AND my computer. I get the same NRE for the query. I don't know what else I can try, and this NRE makes no sense to me at all, given I get the results I expect using everything but EF. I'd chalk it up to a bug with EF if I didn't see it working previously.

Has anyone dealt with this? Searching online for this specific set of circumstances has produced nothing.

Stack Trace :

at System.Data.Entity.Core.EntityKey.AddHashValue(Int32 hashCode, Object keyValue)
at System.Data.Entity.Core.EntityKey.GetHashCode()
at System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
at System.Data.Entity.Core.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry)
at System.Data.Entity.Core.Objects.ObjectStateManager.FindEntityEntry(EntityKey key)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet)
at lambda_method(Closure , Shaper )
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()
4
  • Please fully comprehend the question being asked before you flag it as a possible duplicate. Commented Sep 22, 2016 at 15:29
  • Please include a Minimal, Complete, and Verifiable example. Please also read How do I ask a Good Question. As your question is currently written the duplicate for what is an NRE is the only and best solution for your question. You provided no stack trace, no mention of what is actually null (or not for that matter: example is dbcontext null? is dbcontext.Tree null? is the result dbcontext.Tree.ToList() null or throwing the NRE), or any other specifics. Commented Sep 22, 2016 at 15:33
  • So all trees in your data have values for Left and Right? Commented Sep 22, 2016 at 15:51
  • 1
    Igor, you were right. My problem was reversed, however - I had modified a table that the view used, and I had forgotten to refresh the view, making it no longer match what EF expected. Thank you for your help. I'd flag it as the answer, but I seem to be unable to. Commented Sep 22, 2016 at 16:12

1 Answer 1

2

The problem is that your Model (Tree in your example above) has one ore more properties that are not nullable (and possibly also marked as not nullable in the mapping) but the corresponding column in the data store is nullable. This exception would only manifest itself as soon as there was a record being retrieved that had a null value for one of those column(s).

  • Model fix - When updating the model be sure to use Nullable<T> or ? for nullable value types and if you have mappings defined (either via attributes or in types that inherit EntityTypeConfiguration) also specify that the property is optional there.
  • Data store fix - Alternatively change the data store schema and data to align it with what is expected in the model.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.