然后我有一个带有
的databasecontextpublic class DatabaseContext : DbContext {
public DbSet<A> aList {get;set;}
}
过去,当没有
class D
时,我像这样加载了数据库:
DatabaseContext _databaseContext = new DatabaseContext();
List<A> localA = _databaseContext.aList.Include("bList")
.Include("bList.cList")
.ToList();
class D
我尝试了以下内容:
List<A> localA = _databaseContext.aList.Include("bList")
.Include("bList.cList")
.Include("bList.dList")
.ToList();
但是它抛出了一个
System.NullReferenceException: 'Object reference not set to an instance of an object.'
。
如果我删除
Include("bList.cList")
或
Include("bList.dList")
,它再次工作,但只能加载class C
或
class D
。
在我添加两行的时候,我会得到一个例外。我如何在数据库中包括两个类?我可以排除数据库的错别字或访问问题,因为如果我只使用其中一个,则可以正常工作。
我也尝试了
List<A> localA = _databaseContext.aList.Include("bList.cList")
.Include("bList.dList")
.ToList();
和
List<A> localA = _databaseContext.aList.Include(a => a.bList.Select(b => b.cList))
.Include(a => a.bList.Select(b => b.dList))
.ToList();
但他们还抛出了无nullReferenceException。
stacktrace从例外:
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)
试图包括来自已经相关实体的另一个相关实体时,您需要重复::
Include