{"record":{"id":"585402654126809f","repo":"dotnet/efcore","slug":"compilation-failed-with-errors","errorCode":null,"errorMessage":"Compilation failed with errors:","messagePattern":"Compilation failed with errors:","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":381,"sourceCode":"        }\n\n        if (!project.SupportsCompilation)\n        {\n            throw new NotSupportedException(DesignStrings.UncompilableProject(_project));\n        }\n\n        var compilation = project.GetCompilationAsync().GetAwaiter().GetResult()!;\n        var errorDiagnostics = compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();\n        if (errorDiagnostics.Length != 0)\n        {\n            var errorBuilder = new StringBuilder();\n            errorBuilder.AppendLine(DesignStrings.CompilationErrors);\n            foreach (var diagnostic in errorDiagnostics)\n            {\n                errorBuilder.AppendLine(diagnostic.ToString());\n            }\n\n            throw new InvalidOperationException(errorBuilder.ToString());\n        }\n\n        var syntaxGenerator = SyntaxGenerator.GetGenerator(\n            workspace, _language == \"VB\" ? LanguageNames.VisualBasic : _language ?? LanguageNames.CSharp);\n\n        var precompiledQueryCodeGenerator = services.GetRequiredService<IPrecompiledQueryCodeGeneratorSelector>().Select(_language);\n\n        var precompilationErrors = new List<PrecompiledQueryCodeGenerator.QueryPrecompilationError>();\n        var generatedFiles = precompiledQueryCodeGenerator.GeneratePrecompiledQueries(\n            compilation,\n            syntaxGenerator,\n            context,\n            memberAccessReplacements,\n            precompilationErrors,\n            generatedFileNames,\n            assembly: _assembly,\n            suffix);\n","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L363-L399","documentation":"Thrown by DbContextOperations.PrecompileQueries when the Roslyn compilation of the project produces one or more error-severity diagnostics. Before generating precompiled queries, the tool compiles the project; if it does not compile, precompilation cannot proceed and all error diagnostics are listed in the message.","triggerScenarios":"Running query precompilation ('--precompile-queries') on a project that has compile errors. The compilation runs inside PrecompileQueries via project.GetCompilationAsync() and surfaces every DiagnosticSeverity.Error.","commonSituations":"The project does not build cleanly (broken code, missing references, un-restored packages). Running precompilation on a work-in-progress branch with compile errors. CI/local environment where a referenced assembly is missing. A transitive dependency version mismatch causing CS errors.","solutions":["Build the project first ('dotnet build') and fix every compile error before running optimize.","Restore packages ('dotnet restore') so referenced assemblies are present.","Ensure the same configuration used at runtime is used at design time (e.g., define constants, target framework).","Read the listed diagnostics in the error message to locate and fix each CS error."],"exampleFix":"# before - project has compile errors\ndotnet ef dbcontext optimize --precompile-queries  # lists 'Compilation failed with errors:'\n\n# after - build clean first, then optimize\ndotnet build\ndotnet ef dbcontext optimize --precompile-queries","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ops.Optimize(..., precompileQueries: true, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Compilation failed with errors\"))\n{\n    // read the listed CS diagnostics, fix each compile error, rebuild, then retry\n}","preventionTips":["Always build the project clean ('dotnet build') before running query precompilation.","Restore packages so referenced assemblies are present.","Keep the design-time and runtime configurations aligned (target framework, defines)."],"tags":["design","precompilation","compilation","roslyn","build-errors"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}