Eager加载导航属性抛出异常 - 提供者在EntityFramework.Core中不能为null

时间:2016-01-14 15:53:13

标签: asp.net entity-framework asp.net-core entity-framework-core

我目前正在ASP.NET 5和Entity Framework 7.0.0-rc1-final上编写应用程序。我试图使用急切加载的多级导航属性执行查询,这会导致以下异常(实际上,提供程序在EntityFramework.Core中不能为null):

System.Reflection.TargetInvocationException was unhandled by user code
  HResult=-2146232828
  Message=Exception has been thrown by the target of an invocation.
  Source=mscorlib
  StackTrace:
       at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
       at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
       at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)
       at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.CreateEntityQueryable(IEntityType targetEntityType)
       at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.CreateJoinsForNavigations(QuerySourceReferenceExpression outerQuerySourceReferenceExpression, IEnumerable`1 navigations)
       at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.<>c__DisplayClass13_0.<VisitMember>b__0(IEnumerable`1 ps, IQuerySource qs)
       at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpressionCore[TResult](MemberExpression memberExpression, IQuerySource querySource, Func`3 memberBinder)
       at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindNavigationPathMemberExpression[TResult](MemberExpression memberExpression, Func`3 memberBinder)
       at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.VisitMember(MemberExpression memberExpression)
       at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
       at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
       at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
       at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection`1 nodes, Func`2 elementVisitor)
       at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
       at System.Linq.Expressions.MemberInitExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
       at Remotion.Linq.Clauses.SelectClause.TransformExpressions(Func`2 transformation)
       at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.NavigationRewritingQueryModelVisitor.VisitSelectClause(SelectClause selectClause, QueryModel queryModel)
       at Remotion.Linq.Clauses.SelectClause.Accept(IQueryModelVisitor visitor, QueryModel queryModel)
       at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel)
       at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.Rewrite(QueryModel queryModel)
       at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel)
       at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
       at Microsoft.Data.Entity.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
       at Microsoft.Data.Entity.Query.Internal.QueryCompiler.<>c__DisplayClass18_0`1.<CompileQuery>b__0()
       at Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
       at Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery[TResult](Expression query)
       at Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
       at Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
       at Remotion.Linq.QueryableBase`1.GetEnumerator()
       at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
       at MGR.Web.Repositories.StudyRepository.GetAll(Int32 skip, Int32 take, String search) in C:\Projects\MGR\MGR.Web\Repositories\StudyRepository.cs:line 44
       at MGR.Web.Services.StudyService.GetStudies(Int32 skip, Int32 take, String search) in C:\Projects\MGR\MGR.Web\Services\StudyService.cs:line 43
       at MGR.Web.Controllers.HomeController.SearchStudies(IDataTablesRequest request) in C:\Projects\MGR\MGR.Web\Controllers\HomeController.cs:line 41
  InnerException: 
       HResult=-2147467261
       Message=Value cannot be null.
Parameter name: provider
       ParamName=provider
       Source=EntityFramework.Core
       StackTrace:
            at Microsoft.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
            at Microsoft.Data.Entity.Query.Internal.EntityQueryable`1..ctor(IAsyncQueryProvider provider)
            at Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor._CreateEntityQueryable[TResult](IAsyncQueryProvider entityQueryProvider)
       InnerException: 

要创建DbContext和实体,我使用了dnx ef脚手架,它创建了以下(此处略)研究对象和相关属性:

public partial class Study
{
  public virtual Project Project {get;set;}
}

项目类:

public partial class Project
{
  public virtual Category Category {get;set;}
}

正如您所看到的,关于实体配置,一切都非常标准。

最后,下面是查询:

return _db.Study.Where(s => s.MasterStudyId != null).Skip(skip).Take(take)
                .Include(s => s.Project.Category).Select(s => new Models.StudySearchResult
            {
                StudyId = s.StudyId,
                MasterStudyId = s.MasterStudyId,
                ShortTitle = s.ShortTitle,
                Category = s.Project.Category.CategoryDesc,
                CreatedByUserId = s.SubmitterId,
                StudyDisplayId = String.IsNullOrEmpty(s.RhId) ? "DRAFT" + s.StudyId.ToString().PadLeft(5, '0') : s.RhId
            }).ToList();

如果从查询投影中删除s.Project.Category.CategoryDesc(导航属性),则查询会正确执行。

我已经尝试了一些Include()版本,包括我在相关帖子中找到的Include(s => s.Project).ThenInclude(p => p.Category),但没有解决问题。我相信导航属性在EF7 RC1中工作,任何人都可以证明我错了或提供工作代码吗?

我很高兴收录更多细节。

0 个答案:

没有答案