{"record":{"id":"2e59e031108e7d70","repo":"dotnet/efcore","slug":"missing-default-arm-for-switch-expression","errorCode":null,"errorMessage":"Missing default arm for switch expression","messagePattern":"Missing default arm for switch expression","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs","lineNumber":2257,"sourceCode":"                    var result = translatedBody switch\n                    {\n                        BlockSyntax block => SingletonList<StatementSyntax>(block.WithStatements(block.Statements.Add(BreakStatement()))),\n                        StatementSyntax s => List([s, BreakStatement()]),\n                        ExpressionSyntax e => List(new StatementSyntax[] { ExpressionStatement(e), BreakStatement() }),\n\n                        _ => throw new ArgumentOutOfRangeException()\n                    };\n\n                    return result;\n                }\n            }\n\n            case ExpressionContext.Expression:\n            case ExpressionContext.ExpressionLambda:\n            {\n                if (switchNode.DefaultBody is null)\n                {\n                    throw new NotSupportedException(\"Missing default arm for switch expression\");\n                }\n\n                var parentLiftedState = _liftedState;\n                _liftedState = parentLiftedState.CreateChild();\n\n                // Translate all arms\n                var arms = SeparatedList(\n                    switchNode.Cases.SelectMany(\n                            c => c.TestValues, (c, tv) => SwitchExpressionArm(\n                                ConstantPattern(Translate<ExpressionSyntax>(tv)),\n                                Translate<ExpressionSyntax>(c.Body)))\n                        .Append(SwitchExpressionArm(DiscardPattern(), Translate<ExpressionSyntax>(switchNode.DefaultBody))));\n\n                // LINQ SwitchExpression supports non-literal labels, which C# does not support. This rewrites the switch as a series of\n                // nested ConditionalExpressions.\n                if (arms.Any(a => a.Pattern is ConstantPatternSyntax cp && !_constantDetector.IsConstant(cp.Expression)))\n                {\n                    _liftedState = parentLiftedState;","sourceCodeStart":2239,"sourceCodeEnd":2275,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Query/Internal/LinqToCSharpSyntaxTranslator.cs#L2239-L2275","documentation":"Thrown by TranslateSwitch when a SwitchExpression is translated in an expression (not statement) context but has a null DefaultBody. C# switch expressions must be exhaustive and require a discard/default arm, so the translator needs a default body to emit; without one it cannot produce valid C#. A statement-context switch (with throw default arm) is handled separately.","triggerScenarios":"A SwitchExpression used as a value (ExpressionContext.Expression or ExpressionContext.ExpressionLambda) where DefaultBody is null, so there is nothing to generate for the implicit discard arm.","commonSituations":"Programmatically constructing Expression.Switch without the defaultBody argument and using the result as a value in a translated query; relying on a switch that is exhaustive by domain knowledge but has no default arm in the expression tree.","solutions":["Provide a default body when constructing the SwitchExpression so DefaultBody is non-null.","Restructure the expression so the switch is used in a statement context rather than as a value.","Replace the switch with a chain of conditional expressions that do not require a default arm."],"exampleFix":"// before\nExpression.Switch(value, cases, defaultBody: null);\n// after\nExpression.Switch(value, cases, defaultBody: Expression.Constant(fallback));","handlingStrategy":"validation","validationCode":"// Ensure every switch used as a value has a default body\nstatic bool ValueSwitchesHaveDefault(Expression e) { /* walk: flag SwitchExpression with DefaultBody==null used as a value */ return true; }","typeGuard":"static bool IsExhaustiveSwitch(SwitchExpression s) => s.DefaultBody is not null;","tryCatchPattern":"try { var csharp = translator.Translate(expr); }\ncatch (NotSupportedException ex) when (ex.Message.Contains(\"Missing default arm\"))\n{ throw new InvalidOperationException(\"Add a default body to the switch expression.\", ex); }","preventionTips":["Always pass a defaultBody to Expression.Switch.","Prefer statement-context switches when a default is genuinely absent.","Replace switches with conditional expressions when feasible."],"tags":["expression-trees","precompiled-queries","compiled-model","linq","switch"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}