EntityFramework dll 中发生了类型为“System.StackOverflowException”的未

本文关键字:System StackOverflowException 的未 dll 发生了 类型 EntityFramework | 更新日期: 2023-09-27 18:35:49

当我尝试处理 270k 条记录时,我收到此异常。它在 12k 时失败。有人可以向我解释我错过了什么吗?

数据库是 SQL,我使用的是 EF 6。我正在使用谓词生成器来构建我的 where 子句。这个想法是从表中选择 *,其中 (a = 'v1' 和 b = 'v2') 或 (a = 'v11' 和 b = 'v21') 或 (a = 'v12' 和 b = 'v22') ..)我没有看到任何地方我仍然持有对代表 EF 类的对象的引用。我正在为要发送回查看的结果创建 POCO。有什么想法吗?

此外,我使用的是 10000 的 CommandTimeout 和它失败的点,当我在 sql 管理工作室中使用相同的参数运行查询时,它会返回 400 行。

当我运行探查器时,我注意到在出现错误前几秒钟,内存使用量飙升至1GB +

谢谢

    public List<SearchResult> SearchDocuments(List<SearchCriteria> searchCriterias)
{
            List<SearchResult> results = new List<SearchResult>();
            var fieldSettings = GetData() ;// make a call to database to get this data
            using (var context = CreateContext())
            {
                var theQuery = PredicateBuilder.False<ViewInSqlDatabase>();
                int skipCount = 0;
                const int recordsToProcessInOneBatch = 100;
                while (searchCriterias.Skip(skipCount).Any())
                {
                    var searchCriteriasBatched = searchCriterias.Skip(skipCount).Take(recordsToProcessInOneBatch);
                    foreach (var searchCriteria in searchCriteriasBatched)
                    {
                        var queryBuilder = PredicateBuilder.True<ViewInSqlDatabase>();
                        // theQuery
                        if (searchCriteria.State.HasValue)
                            queryBuilder = queryBuilder.And(a => a.State == searchCriteria.State.Value);
                        if (!string.IsNullOrWhiteSpace(searchCriteria.StateFullName))
                            queryBuilder = queryBuilder.And(a => a.StateName.Equals(searchCriteria.StateFullName, StringComparison.CurrentCultureIgnoreCase));
                        if (searchCriteria.County.HasValue)
                            queryBuilder = queryBuilder.And(a => a.County == searchCriteria.County.Value);
                        if (!string.IsNullOrWhiteSpace(searchCriteria.CountyFullName))
                            queryBuilder = queryBuilder.And(a => a.CountyName.Equals(searchCriteria.CountyFullName, StringComparison.CurrentCultureIgnoreCase));
                        if (!string.IsNullOrWhiteSpace(searchCriteria.Township))
                            queryBuilder = queryBuilder.And(a => a.Township == searchCriteria.Township);
                        // and so on...for another 10 parameters
                        theQuery = theQuery.Or(queryBuilder.Expand());
                    }
                    // this is where I get error after 12k to 15k criterias have been processed
                    var searchQuery = context.ViewInSqlDatabase.AsExpandable().Where(theQuery).Distinct().ToList();
                    foreach (var query in searchQuery)
                    {
                        var newResultItem = SearchResult.Create(query, fieldSettings); // POCO object with no relation to database
                        if (!results.Contains(newResultItem))
                            results.Add(newResultItem);
                    }
                    skipCount += recordsToProcessInOneBatch;
                }
            }
            return results.Distinct().OrderBy(a => a.State).ThenBy(a => a.County).ThenBy(a => a.Township).ToList();
}

EntityFramework dll 中发生了类型为“System.StackOverflowException”的未

Fourat 是正确的,您可以修改查询以context.SearchResults.Where(x => ((x.a == 'v1' &&x.b == 'v2') || (x.a = 'v11' &&x.b = 'v21') || (x.a = 'v12' && x.b = 'v22')).Distinct().OrderBy(a => a.State).ThenBy(a => a.County).ThenBy(a => a.Township).ToList(); 这与什么 使数据库为您和您完成繁重的工作

如果可以的话,我还建议您使用惰性评估,而不是将其强制放入列表中。