{"record":{"id":"d1ffcb11a6f3f492","repo":"dotnet/efcore","slug":"query-precompilation-failed-with-errors","errorCode":null,"errorMessage":"Query precompilation failed with errors:","messagePattern":"Query precompilation failed with errors:","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":409,"sourceCode":"            compilation,\n            syntaxGenerator,\n            context,\n            memberAccessReplacements,\n            precompilationErrors,\n            generatedFileNames,\n            assembly: _assembly,\n            suffix);\n\n        if (precompilationErrors.Count > 0)\n        {\n            var errorBuilder = new StringBuilder();\n            errorBuilder.AppendLine(DesignStrings.QueryPrecompilationErrors);\n            foreach (var error in precompilationErrors)\n            {\n                errorBuilder.AppendLine(error.ToString());\n            }\n\n            throw new InvalidOperationException(errorBuilder.ToString());\n        }\n\n        var writtenFiles = new List<string>();\n        return CompiledModelScaffolder.WriteFiles(generatedFiles, outputDir);\n    }\n\n    private string? GetNamespaceFromOutputPath(string directoryPath)\n    {\n        var subNamespace = SubnamespaceFromOutputPath(_projectDir, directoryPath);\n        return string.IsNullOrEmpty(subNamespace)\n            ? _rootNamespace\n            : string.IsNullOrEmpty(_rootNamespace)\n                ? subNamespace\n                : _rootNamespace + \".\" + subNamespace;\n    }\n\n    // if outputDir is a subfolder of projectDir, then use each subfolder as a sub-namespace\n    // --output-dir $(projectFolder)/A/B/C","sourceCodeStart":391,"sourceCodeEnd":427,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L391-L427","documentation":"Thrown by DbContextOperations.PrecompileQueries when GeneratePrecompiledQueries populates the precompilationErrors list (count > 0). Unlike project compile errors, these are failures produced by the query-precompilation engine itself while transforming the context's queries into generated C#. All errors are aggregated into the thrown message.","triggerScenarios":"Running query precompilation on a project that compiles fine but whose queries cannot be precompiled by the EF Core precompilation code generator. Each error in the list represents a specific query/transformation that failed during generation.","commonSituations":"Using query patterns the precompiler does not yet support. A bug or limitation in the precompiled-query code generator for the active provider. Version mismatch between the EF Core runtime and design-time packages. Complex queries with unsupported operators/translations for precompilation.","solutions":["Read each error in the aggregated message to identify the failing query and simplify/rewrite that query.","Ensure the EF Core runtime and EFCore.Design versions match exactly.","Try the latest EF Core version; precompilation support is actively expanding.","If a specific query cannot be precompiled, exclude it from precompilation or report the issue to the EF Core team with a repro."],"exampleFix":"// before - a query fails to precompile\ndotnet ef dbcontext optimize --precompile-queries  # 'Query precompilation failed with errors:'\n\n// after - identify the failing query from the message, simplify it, then retry\n// e.g. replace an unsupported GroupBy variant with a supported form","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ops.Optimize(..., precompileQueries: true, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Query precompilation failed\"))\n{\n    // parse the aggregated errors, simplify/rewrite the offending query, then retry\n}","preventionTips":["Keep EF Core runtime and EFCore.Design versions aligned.","Stay on a current EF Core release for the broadest precompilation support.","Avoid exotic query shapes; simplify queries that fail to precompile."],"tags":["design","precompilation","query","codegen"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}