{"record":{"id":"edf1927b0948f69f","repo":"dotnet/efcore","slug":"memberinit-lifted-statements","errorCode":null,"errorMessage":"MemberInit: lifted statements","messagePattern":"MemberInit: lifted statements","errorType":"exception","errorClass":"NotImplementedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2550,"sourceCode":"            if (binding is MemberListBinding listBinding\n                && (!listBinding.Member.GetMemberType().IsAssignableTo(typeof(IEnumerable))\n                    || listBinding.Initializers.Any(e => e.AddMethod.Name != \"Add\" || e.Arguments.Count != 1)))\n            {\n                incompatibleListBindings ??= [];\n                incompatibleListBindings.Add(listBinding);\n                continue;\n            }\n\n            var liftedStatementsPosition = _liftedState.Statements.Count;\n\n            VisitMemberBinding(binding);\n\n            initializerExpressions.Add((AssignmentExpressionSyntax)Result!);\n\n            if (_liftedState.Statements.Count > liftedStatementsPosition)\n            {\n                // TODO: This is tricky because of the recursive nature of MemberMemberBinding\n                throw new NotImplementedException(\"MemberInit: lifted statements\");\n            }\n        }\n\n        if (incompatibleListBindings is not null)\n        {\n            // TODO: Lift the instantiation and add extra statements to add the incompatible bindings after that\n            throw new NotImplementedException(\"MemberInit: incompatible MemberListBinding\");\n        }\n\n        Result = objectCreation.WithInitializer(\n            InitializerExpression(\n                SyntaxKind.ObjectInitializerExpression,\n                SeparatedList<ExpressionSyntax>(initializerExpressions)));\n\n        return memberInit;\n    }\n\n    /// <inheritdoc />","sourceCodeStart":2532,"sourceCodeEnd":2568,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2532-L2568","documentation":"While translating a MemberInitExpression (object initializer), if translating a member binding produces lifted statements (side-effecting sub-expressions that must be hoisted before the initializer), the translator cannot place them correctly and throws NotImplementedException(\"MemberInit: lifted statements\").","triggerScenarios":"An object initializer where a member value expression has side effects requiring lifting, e.g. new X { A = SomeMethod() } where SomeMethod's translation lifts statements.","commonSituations":"Complex object initializers in precompiled queries with method-call member values or nested expressions that need hoisting.","solutions":["Compute side-effecting member values into locals before the initializer.","Simplify initializer member expressions to be side-effect-free (constants, parameter reads).","Use a constructor plus explicit post-construction assignments instead of an initializer."],"exampleFix":"// before\nvar init = Expression.MemberInit(newX, Expression.Bind(propA, MethodCall())); // lifts -> throws\n// after\nvar tmp = Expression.Parameter(typeof(int), \"a\");\n// assign tmp = MethodCall() before, then bind propA = tmp","handlingStrategy":"validation","validationCode":"// Detect member-init bindings whose value translation would lift statements\npublic sealed class MemberInitSideEffectDetector : ExpressionVisitor {\n    public bool Found;\n    protected override Expression VisitMemberInit(MemberInitExpression mi) {\n        foreach (var b in mi.Bindings) {\n            if (b is MemberAssignment ma && new SideEffectDetector().HasSideEffects(ma.Expression)) Found = true;\n        }\n        return mi;\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Compute side-effecting member values into locals before the initializer.","Keep initializer member expressions side-effect-free.","Prefer constructor + explicit assignments for complex initialization."],"tags":["ef-core","linq","expression-tree","object-init","code-generation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}