【问题标题】:Why am I getting a NullReferenceException in System.Data.Entity.dll (Entity Framework)?为什么我在 System.Data.Entity.dll(实体框架)中收到 NullReferenceException?
【发布时间】:2012-05-11 07:34:57
【问题描述】:

我有一个List<State>State 实体对象,每个State 对象都有其他对象的集合,例如LicensesTaxesBonds

还有一个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

当我选择 Nevada 并尝试将 DataGrid 绑定到 ExpiredObjects 集合时,我也遇到了完全相同的错误(如果我注释掉该绑定,代码可以正常工作)

有谁知道是什么导致了这个错误?

【问题讨论】:

  • 如果只针对内华达那肯定是数据问题,彻底检查一下db。
  • @HenkHolterman 抱歉,我不确定哪些信息是相关的。这是分贝优先。所有对象(StateLicenseTax 等)都有自己的数据库表,外键链接它们。他们也有自己的主键,没有任何继承。我现在正在研究数据 - 不知道为什么我之前没想过这样做
  • @HenkHolterman 没错,问题出在数据上。我拉出过期对象的过程正在为 EF 中标记为非空的字段之一返回 null 值,因此每当它尝试从数据库中获取该对象时,它都会引发错误。谢谢,如果您将其发布为答案,我会接受:)

标签: c# wpf entity-framework


【解决方案1】:

如果只针对内华达,那一定是数据问题,彻底检查数据库。

总而言之,核心问题是:

  • 这是 db-first。
  • ...正在为 EF 中标记为非空的字段之一返回空值

【讨论】:

  • 哦,伙计,这需要一段时间才能找到,但它让我朝着正确的方向前进。我遇到了同样的错误,我的表数据很好,但是我有一个基于 sql 视图的实体对象,该对象的连接不正确,插入了一条空记录,杀死了整个事情。谢谢您的帮助。 +1 无处不在。
  • 非常感谢!
【解决方案2】:

我在模块化架构中多次意外加载模块时遇到了同样的问题。

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多