{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L363-L399","documentation":"InvalidOperationException thrown after the project compiles in Roslyn but GetCompilationAsync returns diagnostics with Severity == Error. The error messages from each diagnostic are appended. Precompilation is aborted because the source does not compile.","triggerScenarios":"Running `dotnet ef dbcontext optimize --precompile-queries` against a project that has C# compile errors (missing references, syntax errors, type errors). The errors are reported before any query code is generated.","commonSituations":"Pending compile errors in the working tree, missing package references that the IDE tolerates but the command-line compiler rejects, wrong target framework, or a stale obj/ from a previous build.","solutions":["Build the project with `dotnet build` and resolve every error first.","Restore packages (`dotnet restore`) so missing references resolve.","Clean stale artifacts: `dotnet clean` then retry.","Read each diagnostic in the thrown message and fix at its source location."],"exampleFix":"# before\n> dotnet ef dbcontext optimize --precompile-queries   # fails on CS errors\n\n# after\n> dotnet build src/MyApp.Data\n# fix any errors reported, then\n> dotnet ef dbcontext optimize --precompile-queries","handlingStrategy":"validation","validationCode":"// ensure the project compiles with zero errors before optimize\nif (await Process.RunAsync(\"dotnet\", $\"build {projectPath} --no-restore\") != 0)\n    throw new InvalidOperationException(\"Project has compile errors; resolve before optimize.\");","typeGuard":null,"tryCatchPattern":"try { operations.Optimize(..., precompileQueries: true, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.StartsWith(\"Compilation failed\"))\n{ /* each diagnostic in ex.Message must be fixed at its source */ }","preventionTips":["Keep the working tree compiling (`dotnet build`) before running optimize.","Restore packages so references resolve.","Run `dotnet clean` after SDK changes to avoid stale obj/."],"tags":["design-time","ef-tools","precompilation","compilation","roslyn"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}