dotnet/efcore · error · NotSupportedException

The project ' ' does not support compilation.

Error message

The project '{project}' does not support compilation.

What it means

NotSupportedException thrown when project.SupportsCompilation is false after the project loaded successfully. Query precompilation needs Roslyn to compile the loaded project; if the project kind does not support compilation (e.g. a solution folder or unsupported project type), precompilation is impossible.

Solutions

  1. Target the concrete C#/VB project (the .csproj with Compile items), not a wrapper or folder project.
  2. Ensure a single target framework is selected when multi-targeting.
  3. Disable precompilation (--no-precompile-queries) if the project legitimately cannot be compiled by Roslyn.

Example fix

# before
> dotnet ef dbcontext optimize --precompile-queries --project src/MyApp.sln

# after
> dotnet ef dbcontext optimize --precompile-queries --project src/MyApp.Data/MyApp.Data.csproj
Defensive patterns

Strategy: validation

Validate before calling

// before invoking, ensure the project file is a compilable C#/VB project
var proj = await MSBuildWorkspace.Create().OpenProjectAsync(projectPath);
if (!proj.SupportsCompilation)
    throw new NotSupportedException($"{projectPath} is not a compilable project.");

Try / catch

try { operations.Optimize(..., precompileQueries: true, ...); }
catch (NotSupportedException ex) when (ex.Message.Contains("does not support compilation"))
{ /* point the tool at the real .csproj */ }

Prevention

When it happens

Trigger: Pointing optimize/precompilation at a project that MSBuildWorkspace loads but cannot compile — typically a non-C#/non-VB project, a project with no Compile items, or a meta project.

Common situations: Running tools from a shared/empty project, an MSBuild SDK project EF tools do not recognize, or a multi-targeting project where the inner framework project was not selected.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/39e72bd28614d815. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Design/Design/Internal/DbContextOperations.cs:367

        }
        catch (Exception ex)
        {
            if (workspace != null && !workspace.Diagnostics.IsEmpty)
            {
                var diagnosticMessages = Environment.NewLine
                    + string.Join(
                        Environment.NewLine,
                        workspace.Diagnostics.Select(d => $"  {d.Kind}: {d.Message}"));
                _reporter.WriteVerbose(DesignStrings.MSBuildWorkspaceDiagnostics(diagnosticMessages));
            }

            throw new InvalidOperationException(
                DesignStrings.QueryPrecompilationProjectLoadFailed(_project, ex.Message), ex);
        }

        if (!project.SupportsCompilation)
        {
            throw new NotSupportedException(DesignStrings.UncompilableProject(_project));
        }

        var compilation = project.GetCompilationAsync().GetAwaiter().GetResult()!;
        var errorDiagnostics = compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).ToArray();
        if (errorDiagnostics.Length != 0)
        {
            var errorBuilder = new StringBuilder();
            errorBuilder.AppendLine(DesignStrings.CompilationErrors);
            foreach (var diagnostic in errorDiagnostics)
            {
                errorBuilder.AppendLine(diagnostic.ToString());
            }

            throw new InvalidOperationException(errorBuilder.ToString());
        }

        var syntaxGenerator = SyntaxGenerator.GetGenerator(
            workspace, _language == "VB" ? LanguageNames.VisualBasic : _language ?? LanguageNames.CSharp);

View on GitHub (pinned to 3a2006ef56)