In System.Data.Entity.dll (Entity Framework), why am I receiving a NullReferenceException?

c# entity-framework wpf


I possessList<State> of State object entities, and eachState object includes collections of other items, includingLicenses , Taxes , Bonds , etc

Additionally, there is a collectionExpiredObjects This contains a list of all items that have expired and must be renewed. Somehow, this place is making me feel uneasy.NullReferenceException when I attempt to access it for a single state (Nevada), but I cannot for the life of me understand what is happeningnull given that I can't see anynull values at any place.

This is the code I use to raise the exception. All the states are iterated through in a loop, and all theExpiredObjects to an exhibited view-specific collection. There is still my test code.

    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
                state.Name, state == null, state.ExpiredObjects == null));

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

                // This used to be the only line of code before I started testing
                // It fails on Nevada
            catch (Exception ex)
        return list;

The error's stack trace looks like 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

When I choose Nevada and it attempts to bind a connection, I too have the same issue.DataGrid to theExpiredObjects collection (if I comment out that binding, the code works fine)

Anyone with knowledge of the potential cause of this error?

5/1/2012 7:50:52 PM

Accepted Answer

Make a comprehensive check of the database if it just affects Nevada in order to rule out a data issue.

In conclusion, the main issue was:

  • Db-first was used.
  • ...was supplying a null value for a field that was designated as non null in EF.
5/1/2012 8:33:58 PM

Popular Answer

When I unintentionally loaded a Module more than once in a modular architecture, I had the identical issue.

Additionally, the Module(s) used two identical copies ofDbContext attempting to establish a connection between an entity from one context and an entity from another context.

This concealed theInvalidOperationException Without the module stuff, this generally happens (cannot use an Entity in several Trackers).

I spent three hours figuring it out; maybe others with the similar issue would find it helpful.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow