内部的EF Core 2.1道具导致空引用

时间:2018-06-22 19:11:06

标签: .net-core entity-framework-core ef-core-2.0 ef-core-2.1

当我从EF 1.1迁移到2.1时,我在where条件内进行迭代时开始出现空引用错误,似乎在执行位置未加载StatusCashAdvanceList。

CashAdvance.cs:

Company_Name

堆栈跟踪:

Company_ID

空引用:

public virtual ICollection<StatusCashAdvance> StatusCashAdvanceList { get; set; }

这是在Where中使用的道具方法

NullReferenceException: Object reference not set to an instance of an object.
lambda_method(Closure , CashAdvance)
System.Linq.Enumerable+WhereSelectEnumerableIterator<TSource, TResult>.MoveNext()
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider._TrackEntities<TOut, TIn>(IEnumerable<TOut> results, QueryContext queryContext, IList<EntityTrackingInfo> entityTrackingInfos, IList<Func<TIn, object>> entityAccessors)+MoveNext()
Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider+ExceptionInterceptor<T>+EnumeratorExceptionInterceptor.MoveNext()
System.Collections.Generic.List<T>.AddEnumerable(IEnumerable<T> enumerable)
System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)
[------].CashAdvanceController.Index() in CashAdvanceController.cs
+
            var cashAdvances= _context.CashAdvance

这可行,但是我认为这只是一种解决方法:

var cashAdvances = _context.CashAdvance
                .Include(a => a.StatusCashAdvanceList)
                    .ThenInclude(s => s.ObjTypeStatusCashAdvance)
                .Where(a => a.ActualStatusCashAdvance.IdTypeStatusCashAdvance < PAYED)                
                .ToList();

0 个答案:

没有答案