[英]Entity Framework 6 query generates NullReferenceException
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.编辑:在 Linqpad 中添加了来自 EF 的堆栈跟踪。
Using EF6, I have a very simple query :使用 EF6,我有一个非常简单的查询:
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. dbcontext.Tree
是 SQL Server 2012 中的一个视图。使用 Linqpad5,我使用其内置连接获得了预期的结果。 Setting up an EF connection to my project, I get the NRE.建立到我的项目的 EF 连接,我得到了 NRE。 Checking the SQL, I can copy and paste that into a SQL query window and get the proper results.
检查 SQL,我可以将其复制并粘贴到 SQL 查询窗口中并获得正确的结果。 I get an NRE without the Where call, also.
我也得到了一个没有 Where 电话的 NRE。
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.
我已经重新启动了 Visual Studio 和我的电脑。 I get the same NRE for the query.
我为查询获得了相同的 NRE。 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.
我不知道我还能尝试什么,而且这个 NRE 对我来说根本没有意义,因为我得到了我期望使用除 EF 之外的所有结果的结果。 I'd chalk it up to a bug with EF if I didn't see it working previously.
如果我以前没有看到它工作,我会把它归结为 EF 的一个错误。
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()
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.问题是您的模型(上面示例中的
Tree
)有一个或多个不可为空的属性(并且可能在映射中也标记为不可为空),但数据存储中的相应列可以为空。 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).只有当检索到的记录为这些列之一具有
null
值时,才会出现此异常。
Nullable<T>
or ?
Nullable<T>
或?
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.EntityTypeConfiguration
类型中)还指定该属性是可选的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.