编辑:这不是无知的基本编程(如试图解引用空对象引用)的问题。实体框架6查询生成NullReferenceException
编辑:在Linqpad中添加了来自EF的堆栈跟踪。
使用EF6,我有一个非常简单的查询:
var menu = dbcontext.Tree.Where(t => t.Left > 2 && t.Right < 10).ToList();
这个工作的权利,直到它神秘停止。 dbcontext.Tree
是SQL Server 2012中的一个视图。使用Linqpad5,我得到了我期望使用其内置连接的结果。建立一个EF连接到我的项目,我得到了NRE。检查SQL,我可以将其复制并粘贴到SQL查询窗口中,并获得正确的结果。我也可以在没有Where调用的情况下获得NRE。
我试过从数据库更新我的模型来刷新任何东西。我尝试从模型中删除视图并进行更新。我试过完全删除模型并重新创建它。我重新启动了Visual Studio和我的电脑。我得到相同的查询NRE。我不知道还有什么我可以尝试,而且这个NRE对我来说毫无意义,因为我得到了我期望使用除EF之外的所有结果的结果。如果我之前没有看到它的话,我会把它记录为EF的一个错误。
有没有人处理过这个?在线搜索这种特定的情况并没有产生任何结果。
堆栈跟踪:
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()
请在将其标记为可能的副本之前,充分理解被问到的问题。 – Pfhoenix
请包括[最小,完整和可验证示例](http://stackoverflow.com/help/mcve)。另请阅读[我如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)。由于您的问题目前已经编写,所以NRE的副本是您问题的唯一和最佳解决方案。您提供了没有堆栈跟踪,没有提及什么是实际为空(或不是这个问题:例如'dbcontext' null?是'dbcontext.Tree' null?是结果dbcontext.Tree.ToList()null或抛出NRE )或其他任何细节。 – Igor
因此,数据中的所有树都具有“Left”和“Right”的值? – trailmax