{"record":{"id":"dccd8f24c78bb4e5","repo":"dotnet/efcore","slug":"invalidvalueinsetproperty","errorCode":"InvalidValueInSetProperty","errorMessage":"The following lambda argument to 'SetProperty' does not represent a valid value: '{valueExpression}'.","messagePattern":"The following lambda argument to 'SetProperty' does not represent a valid value: '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs","lineNumber":749,"sourceCode":"                if (jsonQuery.Path is [])\n                {\n                    translatedSetters.Add(new ColumnValueSetter(jsonColumn, serializedValue));\n                }\n                else\n                {\n                    GenerateJsonPartialUpdateSetterWrapper(jsonQuery, jsonColumn, serializedValue);\n                }\n            }\n\n            SqlExpression TranslateScalarSetterValueSelector(\n                ShapedQueryExpression source,\n                Expression valueSelector,\n                Type type,\n                RelationalTypeMapping typeMapping)\n                => TranslateSetterValueSelector(source, valueSelector, type) is SqlExpression translatedSelector\n                    // Apply the type mapping of the column (translated from the property selector above) to the value\n                    ? _sqlExpressionFactory.ApplyTypeMapping(translatedSelector, typeMapping)\n                    : throw new InvalidOperationException(RelationalStrings.InvalidValueInSetProperty(valueSelector.Print()));\n\n            Expression TranslateSetterValueSelector(\n                ShapedQueryExpression source,\n                Expression valueSelector,\n                Type propertyType)\n            {\n                var remappedValueSelector = valueSelector is LambdaExpression lambdaExpression\n                    ? RemapLambdaBody(source, lambdaExpression)\n                    : valueSelector;\n\n                if (remappedValueSelector.Type != propertyType)\n                {\n                    remappedValueSelector = Expression.Convert(remappedValueSelector, propertyType);\n                }\n\n                var result = _sqlTranslator.TranslateProjection(remappedValueSelector, applyDefaultTypeMapping: false);\n\n                return result is null","sourceCodeStart":731,"sourceCodeEnd":767,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs#L731-L767","documentation":"TranslateScalarSetterValueSelector (line 741-749) translates the value (right-hand) side of a SetProperty that targets a scalar column. If the SQL translator cannot produce a SqlExpression for the value, InvalidValueInSetProperty is thrown, reporting the value expression text. This is the scalar-column counterpart of the property-selector errors.","triggerScenarios":"The value lambda for a column SetProperty references something untranslatable: a client-side method, a property not in the model, a complex operation EF cannot render in SQL, or an unsupported CLR construct.","commonSituations":"Calling a custom C# method or static helper in the value lambda; using DateTime.Now or random values that cannot be parameterized cleanly; referencing unmapped properties; using unsupported string/format methods.","solutions":["Move the computation out of the query, capture the result in a local variable, and pass it as a parameter (EF parameterizes captured locals).","Use only EF-translatable constructs (EF.Functions, supported methods) in the value lambda.","If a server-side function is needed, map it via HasDbFunction or use raw SQL."],"exampleFix":"// before (client-side method in the value lambda)\ndb.Users.ExecuteUpdate(s => s.SetProperty(\n    u => u.Code, u => CodeGenerator.Make(u.Id)));\n\n// after (compute client-side, pass as a captured parameter)\nvar code = CodeGenerator.Make(targetId);\ndb.Users.Where(u => u.Id == targetId)\n    .ExecuteUpdate(s => s.SetProperty(u => u.Code, code));","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { await q.ExecuteUpdateAsync(s => s.SetProperty(e => e.Code, e => Helper.Make(e.Id))); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"valid value\"))\n{ /* compute the value client-side and pass as a parameter */ }","preventionTips":["Keep value lambdas free of client-side method calls.","Capture computed results in locals so EF parameterizes them.","Use EF.Functions or HasDbFunction for server-side operations."],"tags":["efcore","executeupdate","setproperty","query-translation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}