dotnet/efcore · error · InvalidOperationException

InvalidValueInSetProperty

InvalidValueInSetProperty

Error message

The following lambda argument to 'SetProperty' does not represent a valid value: '{valueExpression}'.

What it means

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.

Source

Thrown at src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs:749

                if (jsonQuery.Path is [])
                {
                    translatedSetters.Add(new ColumnValueSetter(jsonColumn, serializedValue));
                }
                else
                {
                    GenerateJsonPartialUpdateSetterWrapper(jsonQuery, jsonColumn, serializedValue);
                }
            }

            SqlExpression TranslateScalarSetterValueSelector(
                ShapedQueryExpression source,
                Expression valueSelector,
                Type type,
                RelationalTypeMapping typeMapping)
                => TranslateSetterValueSelector(source, valueSelector, type) is SqlExpression translatedSelector
                    // Apply the type mapping of the column (translated from the property selector above) to the value
                    ? _sqlExpressionFactory.ApplyTypeMapping(translatedSelector, typeMapping)
                    : throw new InvalidOperationException(RelationalStrings.InvalidValueInSetProperty(valueSelector.Print()));

            Expression TranslateSetterValueSelector(
                ShapedQueryExpression source,
                Expression valueSelector,
                Type propertyType)
            {
                var remappedValueSelector = valueSelector is LambdaExpression lambdaExpression
                    ? RemapLambdaBody(source, lambdaExpression)
                    : valueSelector;

                if (remappedValueSelector.Type != propertyType)
                {
                    remappedValueSelector = Expression.Convert(remappedValueSelector, propertyType);
                }

                var result = _sqlTranslator.TranslateProjection(remappedValueSelector, applyDefaultTypeMapping: false);

                return result is null

View on GitHub (pinned to dbf9771522)

Solutions

  1. Move the computation out of the query, capture the result in a local variable, and pass it as a parameter (EF parameterizes captured locals).
  2. Use only EF-translatable constructs (EF.Functions, supported methods) in the value lambda.
  3. If a server-side function is needed, map it via HasDbFunction or use raw SQL.

Example fix

// before (client-side method in the value lambda)
db.Users.ExecuteUpdate(s => s.SetProperty(
    u => u.Code, u => CodeGenerator.Make(u.Id)));

// after (compute client-side, pass as a captured parameter)
var code = CodeGenerator.Make(targetId);
db.Users.Where(u => u.Id == targetId)
    .ExecuteUpdate(s => s.SetProperty(u => u.Code, code));
Defensive patterns

Strategy: validation

Try / catch

try { await q.ExecuteUpdateAsync(s => s.SetProperty(e => e.Code, e => Helper.Make(e.Id))); }
catch (InvalidOperationException ex) when (ex.Message.Contains("valid value"))
{ /* compute the value client-side and pass as a parameter */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/dccd8f24c78bb4e5. Report an issue: GitHub.