{"record":{"id":"e9205a6561979392","repo":"dotnet/efcore","slug":"the-same-parameterexpression-instance-with-name","errorCode":null,"errorMessage":"The same ParameterExpression instance with name '{parameter}' was used as a variable declaration in a block and a nested block inside it. This is not allowed - use different ParameterExpression instances.","messagePattern":"The same ParameterExpression instance with name '(.+?)' was used as a variable declaration in a block and a nested block inside it\\. This is not allowed - use different ParameterExpression instances\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":516,"sourceCode":"        {\n            var (variables, variableNames) = (stackFrame.Variables, stackFrame.VariableNames);\n\n            var uniquifiedName = UniquifyVariableName(parameter.Name ?? \"unnamed\");\n\n            if (blockContext == ExpressionContext.Expression)\n            {\n                if (!_liftedState.Variables.TryAdd(parameter, uniquifiedName))\n                {\n                    throw new NotSupportedException(\"Parameter clash during expression lifting for: \" + parameter.Name);\n                }\n\n                _liftedState.VariableNames.Add(uniquifiedName);\n            }\n            else\n            {\n                if (!variables.TryAdd(parameter, uniquifiedName))\n                {\n                    throw new InvalidOperationException(\n                        DesignStrings.SameParameterExpressionDeclaredAsVariableInNestedBlocks(parameter.Name ?? \"<null>\"));\n                }\n\n                variableNames.Add(uniquifiedName);\n            }\n        }\n\n        var unassignedVariables = block.Variables.ToList();\n\n        var statements = new List<StatementSyntax>();\n        LabeledStatementSyntax? pendingLabeledStatement = null;\n\n        // Now visit the block's expressions\n        for (var i = 0; i < block.Expressions.Count; i++)\n        {\n            var expression = block.Expressions[i];\n            var onLastBlockLine = i == block.Expressions.Count - 1;\n            _onLastLambdaLine = parentOnLastLambdaLine && onLastBlockLine;","sourceCodeStart":498,"sourceCodeEnd":534,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L498-L534","documentation":"Thrown in VisitBlock (statement context) via DesignStrings.SameParameterExpressionDeclaredAsVariableInNestedBlocks when the same ParameterExpression is declared as a variable in both an outer block and a nested block. InvalidOperationException. This mirrors the System.Linq.Expressions rule that variable ParameterExpressions must be unique within their scope chain. Part of EF Core's LINQ-to-C# syntax translation.","triggerScenarios":"Constructing a BlockExpression where the same ParameterExpression appears in .Variables of both the outer block and an inner nested block.","commonSituations":"Manual expression-tree construction reusing a parameter in nested Block variables; this is also invalid per System.Linq.Expressions semantics at runtime.","solutions":["Create separate ParameterExpression instances for the nested block.","If the variable should be shared, declare it only in the outermost block and reference it inside."],"exampleFix":"// before\nvar v = Expression.Variable(typeof(int));\nvar inner = Expression.Block(new[] { v }, expr); // re-declared\nvar outer = Expression.Block(new[] { v }, inner);\n// after\nvar v = Expression.Variable(typeof(int));\nvar inner = Expression.Block(expr);             // do not redeclare\nvar outer = Expression.Block(new[] { v }, inner);","handlingStrategy":"validation","validationCode":"// Detect a ParameterExpression declared in both an outer and nested block\nforeach (var outer in AllBlocks(expr))\n{\n    var outerVars = new HashSet<ParameterExpression>(outer.Variables);\n    foreach (var inner in outer.Expressions.OfType<BlockExpression>())\n        foreach (var v in inner.Variables)\n            if (outerVars.Contains(v))\n                throw new InvalidOperationException(\n                    $\"ParameterExpression '{v.Name}' is declared in both an outer and nested block.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Declare a variable ParameterExpression only once, in the outermost block that needs it.","Use an ExpressionVisitor to validate scope uniqueness before handing trees to the translator."],"tags":["efcore","expression-trees","parameterexpression","block","scope"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}