EF7 / Linq - 带有嵌套对象的“包含”查询抛出异常

时间:2016-06-24 15:17:13

标签: c# entity-framework linq entity-framework-core

我有以下课程:

public class ActivityEntry 
{      
    public int Id { get; set; }

    public int CostCenterId { get; set; }
    [ForeignKey("CostCenterId")]
    public CostCenter CostCenter { get; set; }
}

public class CostCenter 
{
    public int Id {get;set;}
    public int ActivityId { get; set; }
}

我想查询ActivityEntries确定CostCenter的所有ActivityIds

对于过滤器,我有一个带有activityIds(filterList)的List。以下查询将引发System.InvalidOperationException

ErrorMessage

  

“序列包含多个元素”

我不明白为什么。

var filterList = new List<int> {1};

var data = _db.ActivityEntry
              .Include(x => x.CostCenter)
              .Where(x => filterList.Contains(x.CostCenter.ActivityId))
              .ToList();

另一个不使用嵌套对象WHERE的{​​{1}}语句工作正常。

CostCenter

我是否需要使用其他语法来查询.Where(x =>filterList.Contains(x.CostCenterId)) 对象中的activity-Ids?

堆栈追踪:

  

bei System.Linq.Enumerable.Single [TSource](IEnumerable CostCenter 1.b__0(IEnumerable 1 source) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.<>c__DisplayClass79_0 3 memberBinder)      bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression [TResult](MemberExpression memberExpression,IQuerySource querySource,Func 1 ps, IQuerySource qs) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpressionCore[TResult](MemberExpression memberExpression, IQuerySource querySource, Func 2 memberBinder)      bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitMember(MemberExpression memberExpression)      bei System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)      bei Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(表达式表达式)      bei Remotion.Linq.Clauses.ResultOperators.ContainsResultOperator.TransformExpressions(Func 3 memberBinder) bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.BindMemberExpression(MemberExpression memberExpression, Action 2 transformation)      bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.VisitSubQuery(SubQueryExpression subQueryExpression)      bei Remotion.Linq.Clauses.Expressions.SubQueryExpression.Accept(ExpressionVisitor visitor)      bei Microsoft.Data.Entity.Query.ExpressionVisitors.ExpressionVisitorBase.Visit(表达式表达式)      bei Remotion.Linq.Clauses.WhereClause.TransformExpressions(Func 2 transformation) bei Remotion.Linq.QueryModel.TransformExpressions(Func 2转型)      bei Microsoft.Data.Entity.Query.ExpressionVisitors.Internal.RequiresMaterializationExpressionVisitor.FindQuerySourcesRequiringMaterialization(QueryModel queryModel)      bei Microsoft.Data.Entity.Query.QueryCompilationContext.FindQuerySourcesRequiringMaterialization(EntityQueryModelVisitor queryModelVisitor,QueryModel queryModel)      bei Microsoft.Data.Entity.Query.EntityQueryModelVisitor.CreateQueryExecutor [TResult](QueryModel queryModel)      bei Microsoft.Data.Entity.Storage.Database.CompileQuery [TResult](QueryModel queryModel)   --- EndederStappelüberwachungvomvorhergehenden Ort,dem dieAusnahmeausgelöstwurde---      bei Microsoft.Data.Entity.Query.Internal.QueryCompiler。&lt;&gt; c__DisplayClass18_0 2 transformation) bei Remotion.Linq.QueryModel.TransformExpressions(Func 1编译器)      bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.CompileQuery [TResult](表达式查询)      bei Microsoft.Data.Entity.Query.Internal.QueryCompiler.Execute [TResult](表达式查询)      bei Microsoft.Data.Entity.Query.Internal.EntityQueryProvider.Execute [TResult](表达式表达式)      bei Remotion.Linq.QueryableBase 1.<CompileQuery>b__0() bei Microsoft.Data.Entity.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func 1..ctor(IEnumerable 1.GetEnumerator() bei System.Collections.Generic.List 1来源)      bei zetvnext.application.Services.ReportService.GetCostCenterReportData(FilterDto filter)在C:\ Source \ zetvnext \ src \ zetvnext.application \ Services \ ReportService.cs:Zeile 86。      bei zetvnext.tests.ReportServiceTests.Test_Report_Data()在C:\ Source \ zetvnext \ tests \ zetvnext.tests \ ReportServiceTests.cs:Zeile 142。

1 个答案:

答案 0 :(得分:2)

这是EF7的RC1中的错误,在RC2中已修复。

=&GT; https://github.com/aspnet/EntityFramework/issues/4394