I’m getting an error when running this update to change the rating value of a user.
Can I update a property with a .Count() as part of the value?
// Rating is type decimal
var ratingResult = await _dbContext.Users
.Where(p => p.Id == tipReviewDto.InstructorId)
.ExecuteUpdateAsync(setters => setters
.SetProperty(p => p.Rating, p => ((p.Rating * p.ReceivedReviews.Count()) + tipReviewDto.InstructorRating) / (p.ReceivedReviews.Count() + 1))
);
Here is the error message.
Exception has occurred: CLR/System.InvalidOperationException
*
Exception thrown: ‘System.InvalidOperationException’ in Microsoft.EntityFrameworkCore.Relational.dll: ‘The LINQ expression ‘DbSet()
.Where(u => u.Id == __tipReviewDto_InstructorId_0)
.ExecuteUpdate(setters => setters.SetProperty(
propertyExpression: p => p.Rating,
valueExpression: p => p.Rating * (decimal)p.ReceivedReviews
.Count() + __tipReviewDto_InstructorRating_1 / (decimal)(p.ReceivedReviews
.Count() + 1)))’ could not be translated. Additional information: Translation of member ‘ReceivedReviews’ on entity type ‘User’ failed. This commonly occurs when the specified member is unmapped.
Translation of member ‘ReceivedReviews’ on entity type ‘User’ failed. This commonly occurs when the specified member is unmapped.
The following lambda argument to ‘SetProperty’ does not represent a valid value: ‘p => p.Rating * (decimal)p.ReceivedReviews
.Count() + __tipReviewDto_InstructorRating_1 / (decimal)(p.ReceivedReviews
.Count() + 1)’. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.’
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
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.b__0()