{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L391-L427","documentation":"InvalidOperationException from DbContextOperations when precompilation itself produced errors (the precompilationErrors list is non-empty after GeneratePrecompiledQueries ran). Unlike project-load or compile errors, these come from the query-precompilation pipeline failing to translate/emit individual queries.","triggerScenarios":"`dotnet ef dbcontext optimize --precompile-queries` completes project load and compilation but the precompiled-query generator reports errors while processing LINQ queries in the model (unsupported translation, missing service, AOT-incompatible query).","commonSituations":"Queries that use runtime-only features incompatible with precompilation/AOT, a provider that does not fully support precompiled queries, or a bug in the precompiled query code generator for a specific query shape.","solutions":["Inspect each error in the thrown message — they identify the specific query/expression that failed.","Simplify or rewrite the offending LINQ query to a shape the precompiler supports.","Update the EF Core and provider packages — precompiled query coverage is still maturing.","If a specific query cannot be precompiled, exclude that path from precompilation or disable --precompile-queries and file an issue with the repro."],"exampleFix":"// before - unsupported query operator trips the precompiler\nvar q = db.Set<Order>().Select(o => ComplexTransformation(o));\n\n// after - move the unsupported transform out of the translated LINQ\nvar raw = db.Set<Order>().AsEnumerable();\nvar q = raw.Select(o => ComplexTransformation(o));","handlingStrategy":"try-catch","validationCode":"// run optimize with --no-precompile-queries first to confirm the model compiles\noperations.Optimize(outDir, ..., precompileQueries: false, ...);\n// only then enable precompilation","typeGuard":null,"tryCatchPattern":"try { operations.Optimize(..., precompileQueries: true, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Query precompilation failed\"))\n{ /* inspect each error; simplify the offending query; update provider */ }","preventionTips":["Keep LINQ queries translatable — avoid runtime-only constructs in the translated portion.","Stay on current EF Core/provider versions for precompiled-query coverage.","Isolate precompilation issues by running with --no-precompile-queries."],"tags":["design-time","ef-tools","precompilation","query-translation","native-aot"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}