正如主题所说,我们在EF Core上有一些存储库和工作单元代码,在将.net从3.1升级到6.0(以及EF)后,这些代码出现故障
下面是失败的确切代码。
public async Task<IEnumerable<T>> GetAsync(Expression<Func<T, bool>> filter = null,
Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, params Expression<Func<T, object>>[] includes)
{
IQueryable<T> query = dbSet;
foreach (var include in includes) query = query.Include(include);
if (filter != null) query = query.Where(filter);
if (orderBy != null) query = orderBy(query);
return await query.AsNoTracking().ToListAsync().ConfigureAwait(false);
}
我试过了
return await query.SingleorDefault()
不起作用。
return await query.FirstOrDefault()
效果不太好。
在所有情况下,我都得到以下错误。
System.TypeInitializationException: The type initializer for 'Microsoft.EntityFrameworkCore.EnumerableMethods' threw an exception.
---> System.InvalidOperationException: Sequence contains more than one matching element
at System.Linq.ThrowHelper.ThrowMoreThanOneMatchException()
at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source, Func`2 predicate)
at Microsoft.EntityFrameworkCore.EnumerableMethods..cctor()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Query.Internal.AllAnyToContainsRewritingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.QueryTranslationPreprocessor.Process(Expression query)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at MYProject.DatabaseLayer.Repository.BaseRepository`1.GetAsync(Expression`1 filter, Func`2 orderBy, Expression`1[] includes) in MyProject.DatabaseLayer\Repository\BaseRepository.cs:line 50
1条答案
按热度按时间ruarlubt1#
我有很多话要说,它不适合的评论,所以我希望我没有得到标记下来,因为也没有太多的背景,以了解你正在努力做什么或实现,我会尽量协助一般尽可能.
SingleOrDefault
具体执行它所说的操作。它查找单个项,例如1或默认值0。这意味着如果您的查询匹配多个结果,它将失败并发送异常。另一方面,
FirstOrDefault
将返回第一个结果,如果没有找到,则返回none。我觉得很奇怪,它说
Sequence contains more than one matching element
,但FirstOrDefault
给出同样的问题。你应该再检查一次。我不认为FirstOrDefault
给出这样的错误。我的建议是,您只需将查询中的所有内容打印到日志或任何您希望的方式,然后检查集合中是否存在您正在查询的内容。
同样,在您的问题中添加您想要得到的结果也是很好的,因为您之前使用了
SingleOrDefault
,这表明您只期望一个匹配结果。