I have the following classes:
public class A {
public List<B> bList {get;set;}
public List<X> xList {get;set;}
}
public class X {
// data
}
public class B {
public List<C> cList {get;set;}
public List<D> dList {get;set;}
}
public class C {
// data
}
// New added class D
public class D {
// data
}
then I have a DatabaseContext with
public class DatabaseContext : DbContext {
public DbSet<A> aList {get;set;}
}
In the past, when there was no class D, I loaded my database like this:
DatabaseContext _databaseContext = new DatabaseContext();
List<A> localA = _databaseContext.aList.Include("xList")
.Include("bList")
.Include("bList.cList")
.ToList();
and it worked perfectly.
Now with the added class D I tried the following:
List<A> localA = _databaseContext.aList.Include("xList")
.Include("bList")
.Include("bList.cList")
.Include("bList.dList")
.ToList();
But it throws a System.NullReferenceException: 'Object reference not set to an instance of an object.'.
If I remove either Include("bList.cList") or Include("bList.dList"), it works again, but only loads class C or class D, respectively.
As soon as I add both lines, I get an Exception.
How can I include both classes from the database?
I can rule out typos or access issues to the database, because if I only use one of them, it works.
I also tried
List<A> localA = _databaseContext.aList.Include("xList")
.Include("bList.cList")
.Include("bList.dList")
.ToList();
and
List<A> localA = _databaseContext.aList.Include("xList")
.Include(a => a.bList.Select(b => b.cList))
.Include(a => a.bList.Select(b => b.dList))
.ToList();
but they also throw a NullReferenceException.
StackTrace from the Exception:
at System.Data.Entity.Core.Objects.DataClasses.RelatedEnd.GetOtherEndOfRelationship(IEntityWrapper wrappedEntity)
at System.Data.Entity.Core.Objects.ObjectStateManager.AddEntityToCollectionOrReference(MergeOption mergeOption, IEntityWrapper wrappedSource, AssociationEndMember sourceMember, IEntityWrapper wrappedTarget, AssociationEndMember targetMember, Boolean setIsLoaded, Boolean relationshipAlreadyExists, Boolean inKeyEntryPromotion)
at System.Data.Entity.Core.Objects.ObjectStateManager.UpdateRelationships(ObjectContext context, MergeOption mergeOption, AssociationSet associationSet, AssociationEndMember sourceMember, IEntityWrapper wrappedSource, AssociationEndMember targetMember, IList targets, Boolean setIsLoaded)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.FullSpanAction[TTargetEntity](IEntityWrapper wrappedSource, IList`1 spannedEntities, AssociationEndMember targetMember)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.<>c__DisplayClass12_0`1.<HandleFullSpanCollection>b__0(Shaper state, List`1 spannedEntities)
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ResetCollection(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ResetCollection(Shaper shaper)
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MaterializeRow()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.RowNestedResultEnumerator.MoveNext()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.TryReadToNextElement()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.ReadElement()
at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.ObjectQueryNestedEnumerator.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at DatabaseConnector.DatabaseController.Load(String connectionName)
I found a working solution, but I have no idea why it works.
If I switch positions from
List<A> localA = _databaseContext.aList.Include("xList")
.Include("bList.cList")
.Include("bList.dList")
.ToList();
to
List<A> localA = _databaseContext.aList.Include("bList.cList")
.Include("bList.dList")
.Include("xList")
.ToList();
then I get no Exception.
The class X has no links to other classes and simply holds two integers.
ThenIncludehelp? => learn.microsoft.com/en-us/dotnet/api/…