我有一个State
的List<State>
实体对象,每个State
对象有其他对象的集合,如Licenses
,Taxes
,Bonds
等为什么我在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
收集(如果我注释掉绑定,代码工作正常)
有谁知道什么可能导致此错误?
如果只有内华达它必须是一个数据问题,请在db彻底检查。 –
@HenkHolterman对不起,我不确定什么信息是相关的。这是db-first。所有对象('State',''License','Tax'等)都有自己的数据库表,外键链接它们。他们也有自己的主键,没有任何东西被继承。我正在调查数据 - 不确定为什么我没有想到在 – Rachel
@HenkHolterman之前这样做你说得对,问题在于数据。我有过期的对象的过程是为在EF中标记为非空的字段之一返回一个“空值”,所以每当它尝试从数据库中获取该对象时,都会抛出一个错误。谢谢,如果你张贴作为答案我会接受:) – Rachel