2012-05-01 38 views
9

我有一个StateList<State>实体对象,每个State对象有其他对象的集合,如LicensesTaxesBonds为什么我在System.Data.Entity.dll(实体框架)中得到NullReferenceException?

还有为ExpiredObjects集合,它是在任何对象的列表过期,需要更新。出于某种原因,当我尝试访问某个特定状态(内华达州)时,此属性给了我一个NullReferenceException,但我无法为我的生活找出实际上null,因为我没有看到任何null值任何地方。

这是我的代码,引发异常。它循环遍历所有状态,并将所有ExpiredObjects添加到显示的特定于视图的集合中。我的测试代码仍然包含

private List<ExpiredObject> LoadAllExpiredObjects() 
    { 
     var list = new List<ExpiredObject>(); 
     foreach (var state in States) 
     { 
      // This tells me the state is not null, and neither is state.ExpiredObjects 
      Debug.WriteLine(string.Format("{0}|{1}|{2}", 
       state.Name, state == null, state.ExpiredObjects == null)); 

      try 
      { 
       var x = state.ExpiredObjects; 
       Debug.WriteLine(x == null); 

       // Exception occurs on the "for" line on the Nevada state only 
       // Contents in for loop do not execute 
       foreach (var item in x) 
       { 
        Debug.WriteLine(string.Format("{0}", item)); 
        list.Add(item); 
       } 

       // This used to be the only line of code before I started testing 
       // It fails on Nevada 
       //list.AddRange(state.ExpiredObjects); 
      } 
      catch (Exception ex) 
      { 
       Debug.WriteLine(ex.Message); 
       Debug.WriteLine(ex.StackTrace); 
      } 
     } 
     return list; 
    } 

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

A first chance exception of type 'System.NullReferenceException' occurred in System.Data.Entity.dll 
Object reference not set to an instance of an object. 
    at System.Data.EntityKey.AddHashValue(Int32 hashCode, Object keyValue) 
    at System.Data.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.Objects.ObjectStateManager.TryGetEntityEntry(EntityKey key, EntityEntry& entry) 
    at System.Data.Common.Internal.Materialization.Shaper.HandleEntityAppendOnly[TEntity](Func`2 constructEntityDelegate, EntityKey entityKey, EntitySet entitySet) 
    at lambda_method(Closure , Shaper) 
    at System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) 
    at System.Data.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext() 
    at System.Data.Objects.DataClasses.RelatedEnd.Merge[TEntity](IEnumerable`1 collection, MergeOption mergeOption, Boolean setIsLoaded) 
    at System.Data.Objects.DataClasses.EntityCollection`1.Load(List`1 collection, MergeOption mergeOption) 
    at System.Data.Objects.DataClasses.EntityCollection`1.Load(MergeOption mergeOption) 
    at System.Data.Objects.DataClasses.RelatedEnd.Load() 
    at System.Data.Objects.DataClasses.RelatedEnd.DeferredLoad() 
    at System.Data.Objects.DataClasses.EntityCollection`1.GetEnumerator() 
    at MyNamespace.StatesViewModel.LoadAllExpiredObjects() in C:\Users\me\Desktop\MySolution\StatesViewModel.cs:line 217 

我也得到了完全相同的错误,当我选择内华达州和它试图将DataGrid绑定到ExpiredObjects收集(如果我注释掉绑定,代码工作正常)

有谁知道什么可能导致此错误?

+4

如果只有内华达它必须是一个数据问题,请在db彻底检查。 –

+1

@HenkHolterman对不起,我不确定什么信息是相关的。这是db-first。所有对象('State',''License','Tax'等)都有自己的数据库表,外键链接它们。他们也有自己的主键,没有任何东西被继承。我正在调查数据 - 不确定为什么我没有想到在 – Rachel

+2

@HenkHolterman之前这样做你说得对,问题在于数据。我有过期的对象的过程是为在EF中标记为非空的字段之一返回一个“空值”,所以每当它尝试从数据库中获取该对象时,都会抛出一个错误。谢谢,如果你张贴作为答案我会接受:) – Rachel

回答

14

如果只是为内华达州,那么它必须是一个数据问题,做一个彻底的检查数据库。

,总结,核心问题是:

  • 这是DB-第一。
  • ...为那个被标记在EF为NOT NULL
+1

感谢您发表了这个答案。它结束了数小时的头撞... – Deane

+0

哦,男人,这花了一段时间才发现,但它把我送往正确的方向。我有同样的错误,我的表格数据是好的,但我有一个基于SQL视图的实体对象与不正确的联接插入一个空记录,杀死了整个事情。谢谢您的帮助。 + 1的全部。 –

0

我得到了同样的问题,当在一个模块化的架构意外地加载模块多次领域之一返回空值。

此外,使用同一个DbContext的两个副本的模块试图将一个实体从一个上下文连接到另一个上下文的另一个实体。

这掩盖了InvalidOperationException(不能在多个跟踪器中使用一个实体),通常在没有模块的情况下发生。

我花了3个小时解决,也许它可以帮助人们有相同问题的

相关问题