c# – 为什么我在System.Data.Entity.dll(实体框架)中得到一个NullReferenceException?

前端之家收集整理的这篇文章主要介绍了c# – 为什么我在System.Data.Entity.dll(实体框架)中得到一个NullReferenceException?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个列表<状态>的国家实体对象,每个状态对象都有其他对象的集合,如许可证,税金,债券等

还有一个ExpiredObjects的集合,它是任何已经过期且需要更新的对象的列表.由于某种原因,当我尝试访问一个特定状态(内华达州)时,这个属性给我一个NullReferenceException,但是对于我来说,我不知道实际上是什么,因为我没有看到任何空值任何地方.

这是我的代码抛出异常.它遍历所有状态,并将所有ExpiredObject添加显示的视图特定集合中.我的测试代码仍然包含在内

  1. private List<ExpiredObject> LoadAllExpiredObjects()
  2. {
  3. var list = new List<ExpiredObject>();
  4. foreach (var state in States)
  5. {
  6. // This tells me the state is not null,and neither is state.ExpiredObjects
  7. Debug.WriteLine(string.Format("{0}|{1}|{2}",state.Name,state == null,state.ExpiredObjects == null));
  8.  
  9. try
  10. {
  11. var x = state.ExpiredObjects;
  12. Debug.WriteLine(x == null);
  13.  
  14. // Exception occurs on the "for" line on the Nevada state only
  15. // Contents in for loop do not execute
  16. foreach (var item in x)
  17. {
  18. Debug.WriteLine(string.Format("{0}",item));
  19. list.Add(item);
  20. }
  21.  
  22. // This used to be the only line of code before I started testing
  23. // It fails on Nevada
  24. //list.AddRange(state.ExpiredObjects);
  25. }
  26. catch (Exception ex)
  27. {
  28. Debug.WriteLine(ex.Message);
  29. Debug.WriteLine(ex.StackTrace);
  30. }
  31. }
  32. return list;
  33. }

堆栈跟踪的错误是这样的:

  1. A first chance exception of type 'System.NullReferenceException' occurred in System.Data.Entity.dll
  2. Object reference not set to an instance of an object.
  3. at System.Data.EntityKey.AddHashValue(Int32 hashCode,Object keyValue)
  4. at System.Data.EntityKey.GetHashCode()
  5. at System.Collections.Generic.GenericEqualityComparer`1.GetHashCode(T obj)
  6. at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
  7. at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key,TValue& value)
  8. at System.Data.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key,EntityEntry& entry)
  9. at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate,EntityKey entityKey,EntitySet entitySet)
  10. at lambda_method(Closure,Shaper )
  11. at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)
  12. at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()
  13. at System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 collection,MergeOption mergeOption,Boolean setIsLoaded)
  14. at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection,MergeOption mergeOption)
  15. at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption)
  16. at System.Data.Objects.DataClasses.RelatedEnd.Load()
  17. at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad()
  18. at System.Data.Objects.DataClasses.EntityCollection`1.GetEnumerator()
  19. at MyNamespace.Statesviewmodel.LoadAllExpiredObjects() in C:\Users\me\Desktop\MySolution\Statesviewmodel.cs:line 217

当我选择内华达州,并且尝试将DataGrid绑定到ExpiredObjects集合(如果我注释该绑定,该代码工作正常),我也得到完全相同的错误

有谁知道可能导致这个错误

解决方法

如果只是内华达,那么它必须是数据问题,请仔细检查数据库.

总而言之,核心问题是:

>这是db-first.> …正在为在EF中标记为不为空的字段之一返回一个空值

猜你在找的C#相关文章