{"record":{"id":"2599402ebcc92136","repo":"dotnet/efcore","slug":"a-compilation-must-be-loaded","errorCode":null,"errorMessage":"A compilation must be loaded.","messagePattern":"A compilation must be loaded\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs","lineNumber":94,"sourceCode":"    /// <summary>\n    ///     Translates a Roslyn syntax tree into a LINQ expression tree.\n    /// </summary>\n    /// <param name=\"node\">The Roslyn syntax node to be translated.</param>\n    /// <param name=\"semanticModel\">\n    ///     The <see cref=\"SemanticModel\" /> for the Roslyn <see cref=\"SyntaxTree\" /> of which <paramref name=\"node\" /> is a part.\n    /// </param>\n    /// <returns>A LINQ expression tree translated from the provided <paramref name=\"node\" />.</returns>\n    /// <remarks>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </remarks>\n    public virtual Expression Translate(SyntaxNode node, SemanticModel semanticModel)\n    {\n        if (_compilation is null)\n        {\n            throw new InvalidOperationException(DesignStrings.CompilationMustBeLoaded);\n        }\n\n        Check.DebugAssert(\n            ReferenceEquals(semanticModel.SyntaxTree, node.SyntaxTree),\n            \"Provided semantic model doesn't match the provided syntax node\");\n\n        _semanticModel = semanticModel;\n\n        // Perform data flow analysis to detect all variables flowing into the query (e.g. captured variables)\n        _dataFlowsIn.Clear();\n        foreach (var flowsIn in _semanticModel.AnalyzeDataFlow(node).DataFlowsIn)\n        {\n            _dataFlowsIn[flowsIn] = null;\n        }\n\n        var result = Visit(node);\n\n        Debug.Assert(_parameterStack.Count == 1);","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Query/Internal/CSharpToLinqTranslator.cs#L76-L112","documentation":"`Translate` guards on `_compilation is null` and throws `InvalidOperationException(CompilationMustBeLoaded)` if the translator was never initialized. `Load(compilation, userDbContext, additionalAssembly)` is the only thing that sets `_compilation`, so calling `Translate` first is an ordering bug.","triggerScenarios":"Instantiating `CSharpToLinqTranslator` and calling `Translate(node, semanticModel)` before invoking `Load(...)`. The field is declared `Compilation? _compilation` precisely to support this check.","commonSituations":"A custom host/wrapper that wires up the translator out of order, or a refactor that moved the `Load` call behind a condition that evaluated false.","solutions":["Call `translator.Load(compilation, dbContext, additionalAssembly)` exactly once before any `Translate` call.","Guard the entry point so `Translate` is never reached on an uninitialized instance."],"exampleFix":"// before\nvar expr = translator.Translate(node, semanticModel);\n// after\ntranslator.Load(compilation, dbContext);\nvar expr = translator.Translate(node, semanticModel);","handlingStrategy":"validation","validationCode":"// Guard Translate on the uninitialized state.\nif (translatorIsLoaded) // track whether Load was called\n    translator.Translate(node, semanticModel);\nelse\n    throw new InvalidOperationException(\"Call CSharpToLinqTranslator.Load(...) before Translate.\");","typeGuard":null,"tryCatchPattern":"try { translator.Translate(node, semanticModel); }\ncatch (InvalidOperationException ex) when (ex.Message == \"A compilation must be loaded.\")\n{ /* call translator.Load(...) then retry */ }","preventionTips":["Treat `Load` as a mandatory one-time initialization step for `CSharpToLinqTranslator`.","Centralize translator construction + `Load` in a single factory so callers can never skip it."],"tags":["ef-core","precompiled-queries","roslyn","initialization"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}