{"record":{"id":"e6eec88a9f996c13","repo":"dotnet/efcore","slug":"failed-to-load-project-for-query-precompilation-p","errorCode":null,"errorMessage":"Failed to load project for query precompilation. Project: {project}. Error: {error}","messagePattern":"Failed to load project for query precompilation\\. Project: (.+?)\\. Error: (.+?)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Design/Design/Internal/DbContextOperations.cs","lineNumber":361,"sourceCode":"            workspace.LoadMetadataForReferencedProjects = true;\n#pragma warning disable CS0618 // Obsolete\n            workspace.WorkspaceFailed += (_, e)\n                => _reporter.WriteError(DesignStrings.MSBuildWorkspaceFailure(e.Diagnostic.Kind, e.Diagnostic.Message));\n#pragma warning restore CS0618 // Obsolete\n            project = workspace.OpenProjectAsync(_project).GetAwaiter().GetResult();\n        }\n        catch (Exception ex)\n        {\n            if (workspace != null && !workspace.Diagnostics.IsEmpty)\n            {\n                var diagnosticMessages = Environment.NewLine\n                    + string.Join(\n                        Environment.NewLine,\n                        workspace.Diagnostics.Select(d => $\"  {d.Kind}: {d.Message}\"));\n                _reporter.WriteVerbose(DesignStrings.MSBuildWorkspaceDiagnostics(diagnosticMessages));\n            }\n\n            throw new InvalidOperationException(\n                DesignStrings.QueryPrecompilationProjectLoadFailed(_project, ex.Message), ex);\n        }\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            }","sourceCodeStart":343,"sourceCodeEnd":379,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Design/Design/Internal/DbContextOperations.cs#L343-L379","documentation":"Thrown by DbContextOperations.PrecompileQueries when workspace.OpenProjectAsync fails for the project being optimized. The exception wraps the original error and, when available, includes MSBuild workspace diagnostics. Query precompilation requires loading the project via Roslyn's MSBuildWorkspace, so any MSBuild/SDK load failure surfaces here.","triggerScenarios":"Running 'dotnet ef dbcontext optimize --precompile-queries' (or the EF Core precompilation flow) when MSBuildWorkspace cannot open the project file. Root causes include missing/incorrect MSBuild, project SDK issues, broken project references, or required restore not run.","commonSituations":"Running the EF tools in an environment where MSBuild is not on PATH or the wrong MSBuild is found. Project references a package/SDK that is not restored. Building on a machine without the .NET SDK or with an incompatible version. Corrupted obj/bin artifacts. The project uses a target framework MSBuildWorkspace cannot evaluate.","solutions":["Run 'dotnet restore' on the project before optimizing, then retry.","Ensure the .NET SDK and a compatible MSBuild are installed and on PATH (run 'dotnet --info').","Clean build artifacts ('dotnet clean') and retry.","Read the wrapped exception message and workspace diagnostics (emitted at verbose level) for the specific MSBuild error and resolve it.","Verify the project file is valid and all package references restore successfully."],"exampleFix":"// before\ndotnet ef dbcontext optimize --precompile-queries  // project load fails\n\n// after - restore and clean first\ndotnet restore\ndotnet clean\ndotnet ef dbcontext optimize --precompile-queries --verbose","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { ops.Optimize(..., precompileQueries: true, ...); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"Failed to load project for query precompilation\"))\n{\n    // run 'dotnet restore' / 'dotnet clean', check MSBuild availability, then retry\n}","preventionTips":["Run 'dotnet restore' and 'dotnet clean' before invoking query precompilation.","Ensure the .NET SDK and compatible MSBuild are installed and on PATH.","Keep build artifacts consistent; avoid partial restores.","Run with --verbose to see workspace diagnostics emitted for the failure."],"tags":["design","precompilation","msbuild","roslyn","tooling"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}