Rac*_*hel 9 c# wpf entity-framework
我有一个List<State>的State实体对象,每个State对象具有其他对象的集合,诸如Licenses,Taxes,Bonds等
还有一个集合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;
}
Run Code Online (Sandbox Code Playgroud)
错误的堆栈跟踪是这样的:
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
Run Code Online (Sandbox Code Playgroud)
当我选择内华达并且它试图将a绑定DataGrid到ExpiredObjects集合时,我也得到完全相同的错误(如果我注释掉该绑定,代码工作正常)
有谁知道可能导致此错误的原因是什么?
Hen*_*man 19
如果它仅适用于内华达州,则必须是数据问题,请在数据库中进行彻底检查.
总而言之,核心问题是:
| 归档时间: |
|
| 查看次数: |
10936 次 |
| 最近记录: |