当包括多个级别的类时nullReference coppeption

问题描述 投票:0回答:0

然后我有一个带有

的databasecontext
public 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


c# entity-framework-6
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.