EF7,选择'时出错在' Include',"参数名称:querySource"

时间:2016-03-30 10:01:32

标签: entity-framework linq select include

var emps = db.Employments
            .Include(x => x.Projects).ThenInclude(x => x.Position)
            .Include(x => x.Projects).ThenInclude(x => x.ProjectSkills).ThenInclude(x => x.Skill)
            .Include(x => x.Company)
            .Where(x => x.UserId == "42fc803f-af62-4b20-8e60-18d30707292d")
            .OrderByDescending(x => x.Start)
            .Select(
                x => new
                {
                    EmplId = x.Id,
                    Company = x.Company,
                    Desc = x.Desc,
                    StartDate = x.Start,
                    EndDate = x.End,
                    Projects = x.Projects
                }).ToList();

        return new ContentResult()
        {
            ContentType = new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/json"),
            Content = JsonConvert.SerializeObject(emps)
        };

嗨所有聪明人。 我使用MVC6 EF7从mutilple表中检索数据。上面的代码,我在下面得到一个错误。我是EF7的初学者。不知道参数名称,' querySource'。

System.ArgumentNullException: Value cannot be null.
Parameter name: querySource
   at Microsoft.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
   at Microsoft.Data.Entity.Query.QuerySourceExtensions.HasGeneratedItemName(IQuerySource querySource)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.VisitEntityQueryable(Type elementType)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.EntityQueryableExpressionVisitor.VisitConstant(ConstantExpression constantExpression)
   at System.Linq.Expressions.ConstantExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)
   at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
   at System.Linq.Expressions.UnaryExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)
   at System.Linq.Expressions.ExpressionVisitor.VisitAndConvert[T](ReadOnlyCollection`1 nodes, String callerName)
   at Remotion.Linq.Parsing.RelinqExpressionVisitor.VisitNew(NewExpression expression)
   at System.Linq.Expressions.NewExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(Expression expression)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.ReplaceClauseReferences(Expression expression, IQuerySource querySource, Boolean inProjection)
   at Microsoft.Data.Entity.Query.EntityQueryModelVisitor.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.EntityQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
   at Microsoft.Data.Entity.Query.RelationalQueryModelVisitor.VisitQueryModel(QueryModel queryModel)
   at Microsoft.Data.Entity.Query.Internal.SqlServerQueryModelVisitor.VisitQueryModel(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 ---

我注意到,当我移动" ToList"在OrderByDescending'之后的条款在选择'之前,它的工作原理非常缓慢。我知道将在' ToList'中调用S​​QL语句。点。所以,这绝对不是一个解决方案。有人能指出我错在哪里吗?还是给我一个解决这个问题的想法?我很感激。

 var emps = db.Employments
            .Include(x => x.Projects).ThenInclude(x => x.Position)
            .Include(x => x.Projects).ThenInclude(x => x.ProjectSkills).ThenInclude(x => x.Skill)
            .Include(x => x.Company)
            .Where(x => x.UserId == "42fc803f-af62-4b20-8e60-18d30707292d")
            .OrderByDescending(x => x.Start).ToList()
            .Select(
                x => new
                {
                    EmplId = x.Id,
                    Company = x.Company,
                    Desc = x.Desc,
                    StartDate = x.Start,
                    EndDate = x.End,
                    Projects = x.Projects
                });

Database diagram here

0 个答案:

没有答案