{"record":{"id":"d497a520a5455929","repo":"dotnet/efcore","slug":"the-following-lambda-argument-to-setproperty-doe","errorCode":null,"errorMessage":"The following lambda argument to 'SetProperty' does not represent a valid property to be set: '{propertyExpression}'.","messagePattern":"The following lambda argument to 'SetProperty' does not represent a valid property to be set: '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs","lineNumber":86,"sourceCode":"            // WHERE Id IN (SELECT ...) syntax), since we allow projecting out to arbitrary shapes (e.g. anonymous types) before the\n            // ExecuteUpdate.\n\n            // To rewrite the query, we need to know the primary key properties, which requires getting the entity type.\n            // Although there may be several entity types involved, we've already verified that they all map to the same table.\n            // Since we don't support table sharing of multiple entity types with different keys, simply get the entity type and key from\n            // the first property selector.\n\n            // The following mechanism for extracting the entity type from property selectors only supports simple member access,\n            // EF.Function, etc. We also unwrap casts to interface/base class (#29618). Note that owned IncludeExpressions have already\n            // been pruned from the source before remapping the lambda (#28727).\n            var firstPropertySelector = setters[0].PropertySelector;\n            if (!IsMemberAccess(\n                    RemapLambdaBody(source, firstPropertySelector).UnwrapTypeConversion(out _),\n                    RelationalDependencies.Model,\n                    out var baseExpression)\n                || _sqlTranslator.TranslateProjection(baseExpression) is not StructuralTypeShaperExpression shaper)\n            {\n                throw new InvalidOperationException(RelationalStrings.InvalidPropertyInSetProperty(firstPropertySelector));\n            }\n\n            // TODO: #36336\n            if (shaper.StructuralType is not IEntityType entityType)\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.ExecuteUpdateSubqueryNotSupportedOverComplexTypes(shaper.StructuralType.DisplayName()));\n            }\n\n            if (entityType.FindPrimaryKey() is not { } pk)\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.ExecuteOperationOnKeylessEntityTypeWithUnsupportedOperator(\n                        nameof(EntityFrameworkQueryableExtensions.ExecuteUpdate),\n                        entityType.DisplayName()));\n            }\n\n            // Generate the INNER JOIN around the original query, on the PK properties.","sourceCodeStart":68,"sourceCodeEnd":104,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Query/RelationalQueryableMethodTranslatingExpressionVisitor.ExecuteUpdate.cs#L68-L104","documentation":"TranslateSetters uses the first SetProperty lambda to identify the entity being updated. It remaps the lambda body, unwraps type conversions, and requires IsMemberAccess to succeed and the translated projection to be a StructuralTypeShaperExpression. If the lambda is not a simple member access on the entity (or unwraps to something other than the entity shaper), this throw fires: EF cannot tell which column to set.","triggerScenarios":"context.Blogs.ExecuteUpdate(s => s.SetProperty(b => 5, value)) (constant); SetProperty(b => b.NotAMappedProperty, ...); SetProperty(b => someExternalValue, ...); SetProperty with a lambda that performs computation (b => b.Count + 1) instead of pure member access; a lambda cast to an interface/base class that unwraps to a non-shaper.","commonSituations":"Using computed expressions inside SetProperty; copying a Select lambda into SetProperty; referencing a static or local instead of an entity member; refactoring entities behind interfaces (#29618 edge cases).","solutions":["Make each SetProperty lambda a simple member-access on the entity parameter: SetProperty(e => e.Property, value).","For computed updates, compute the value outside and pass a constant, or use raw SQL.","Ensure the lambda's source parameter is the entity being updated and that the member is a mapped property of that entity.","Avoid casts to interfaces/base classes inside the property selector unless they unwrap cleanly to the entity type."],"exampleFix":"// before (non-member-access selector -> 599)\nawait db.Blogs.ExecuteUpdateAsync(s => s\n    .SetProperty(b => 5, _ => 5)\n    .SetProperty(b => b.Title.ToUpper(), _ => \"X\"));\n// after (simple member access on mapped properties)\nawait db.Blogs.ExecuteUpdateAsync(s => s\n    .SetProperty(b => b.Status, 5)\n    .SetProperty(b => b.Title, \"X\"));","handlingStrategy":"validation","validationCode":"// Validate that a SetProperty selector is a simple member access on the entity parameter.\nstatic bool IsSimpleMemberAccess<T>(Expression<Func<T, object?>> selector)\n{\n    var body = selector.Body;\n    if (body is UnaryExpression u && u.NodeType == ExpressionType.Convert)\n        body = u.Operand;\n    return body is MemberExpression me && me.Expression == selector.Parameters[0];\n}\n\nif (!IsSimpleMemberAccess((Blog b) => b.Title))\n    throw new InvalidOperationException(\"SetProperty selector must be a simple mapped member access on the entity.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Write SetProperty selectors as e => e.MappedProperty only; no computation, no constants.","Compute values outside the lambda and pass them in.","Add a unit test asserting each SetProperty selector is a simple MemberExpression on the entity parameter."],"tags":["efcore","executeupdate","setproperty","lambda","query-translation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}