我有以下(通用)存储库方法:
public async Task<TEntity> GetAsync(Expression<Func<TEntity, bool>> predicate, DateTime? asOf = null, IEnumerable<string> includeProperties = null,
bool trackChanges = false)
{
var query = Set.Include(includeProperties)
.AsOf(asOf);
if (!trackChanges)
{
query = query.AsNoTracking();
}
return await query.SingleAsync(predicate);
}
执行此测试时,它工作正常
[Fact]
public static async Task GetStoffByStoffId_ReturnsEntity_OK_Test()
{
// Arrange
var dbContext = new SaiTestContext();
IRepository<StoffEntity> repository = new Repository<StoffEntity>(dbContext);
const string stoffId = "645106EF59801EE59EB22F1F93673380";
// Act
var entity = await repository.GetAsync(stoff => stoff.StoffId == stoffId);
// Assert
Assert.NotNull(entity);
}
执行此操作时
[Fact]
public static async Task GetStoffByStoffIdPredicate_ReturnsEntity_OK_Test()
{
// Arrange
var dbContext = new SaiTestContext();
IRepository<StoffEntity> repository = new Repository<StoffEntity>(dbContext);
var stoffUpdate = new StoffUpdateEntity
{
StoffId = "645106EF59801EE59EB22F1F93673380"
};
Func<StoffEntity, StoffUpdateEntity, bool> stoffSeletor =
(stoffEntity, updateEntity) => stoffEntity.StoffId == stoffUpdate.StoffId;
// Act
var entity = await repository.GetAsync(stoff => stoffSeletor(stoff, stoffUpdate));
// Assert
Assert.NotNull(entity);
}
正在抛出此异常
System.InvalidOperationException : The LINQ expression 'DbSet<StoffEntity>
.Where(s => Invoke(__stoffSeletor_0, s[StoffEntity], __stoffUpdate_1)
)' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.<VisitMethodCall>g__CheckTranslated|8_0(ShapedQueryExpression translated, <>c__DisplayClass8_0& )
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at EntityFrameworkCore.TemporalTables.Query.AsOfQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at EntityFrameworkCore.TemporalTables.Query.AsOfQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
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.CompiledQueryCache.GetOrAddQuery[TResult](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.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, LambdaExpression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleAsync[TSource](IQueryable`1 source, Expression`1 predicate, CancellationToken cancellationToken)
at Refdata.SAI.Data.Repositories.Repository`1.GetAsync(Expression`1 predicate, Nullable`1 asOf, IEnumerable`1 includeProperties, Boolean trackChanges) in C:\dev\Refdata.SAI\Source\Refdata.SAI.Data\Repositories\Repository.cs:line 75
at Refdata.SAI.Data.Tests.Integration.RepositoryGetByPredicateTests.GetStoffByStoffIdPredicate_ReturnsEntity_OK_Test() in C:\dev\Refdata.SAI\Source\Refdata.SAI.Data.Tests.Integration\RepositoryGetByPredicateTests.cs:line 61
--- End of stack trace from previous location where exception was thrown ---
将函数作为参数传递时,第二项测试可以固定工作吗?
根本原因是我想从泛型类中使用此函数,该类是由专门类中的protected abstract Func<TEntity, TUpdateEntity, bool> EntitySelector { get; }
实现指定的。
答案 0 :(得分:3)
可以将表达式编译为函数,但不能将任意函数转换回表达式。 stoffSeletor应该返回表达式,而不是函数:
Func<StoffUpdateEntity, Expression<Func<StoffEntity, bool>>> stoffSelector =
(stoffUpdate) => ((StoffEntity stoffEntity) => stoffEntity.StoffId == stoffUpdate.StoffId);
然后
// create predicate expression
var predicate = stoffSelector(stoffUpdate);
// use predicate
var entity = await repository.GetAsync(predicate);
或简短版本
var entity = await repository.GetAsync(stoffSelector(stoffUpdate));