为什么会出现在System.Data.Entity.dll(实体框架)一个NullReferenceException? [英] Why am I getting a NullReferenceException in System.Data.Entity.dll (Entity Framework)?

查看:495
本文介绍了为什么会出现在System.Data.Entity.dll(实体框架)一个NullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表<国家> 国家实体对象,每个国家对象有其他对象的集合,如许可证税金债券

I have a List<State> of State entity objects, and each State object has collections of other objects, such as Licenses, Taxes, Bonds, etc

还有为 ExpiredObjects ,这是一个这是过期的,需要被更新的任何对象的列表。出于某种原因,这个属性是给了我一个的NullReferenceException 当我尝试和访问一个特定的状态(内华达州),但我不能为我的生活弄清楚什么是真正的,因为我没有看到任何值的任何地方。

There's also a collection for ExpiredObjects, which is a list of any object which is expired and needs to be renewed. For some reason, this property is giving me a NullReferenceException when I try and access it for one specific state (Nevada), but I can't for the life of me figure out what is actually null since I don't see any null values anywhere.

下面是我的代码抛出异常。它遍历所有状态,并将所有的 ExpiredObjects ,向四周显示视图特定集合。我的测试代码仍然包含

Here's my code that throws the exception. It loops through all the states, and adds all the ExpiredObjects to a view-specific collection which gets displayed. My test code is still included

    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;
    }



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

The stack trace of the error is this:

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 集合(如果我注释掉绑定,代码工作正常)

I also get the exact same error when I select Nevada and it tries to bind a DataGrid to the ExpiredObjects collection (if I comment out that binding, the code works fine)

有谁知道这可能是造成这个错误?

Does anyone know what could be causing this error?

推荐答案

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

If it's only for Nevada then it must be a data problem, do a thorough check in the db.

和总结,核心问题是:


  • 这是DB-第一。

  • ...是返回的被标记EF为not null

这篇关于为什么会出现在System.Data.Entity.dll(实体框架)一个NullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆