dotnet/efcore · error · NotSupportedException

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

Error message

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

What it means

Thrown as NotSupportedException by DbContextOperations.PrecompileQueries when project.SupportsCompilation is false after the project was loaded by MSBuildWorkspace. Query precompilation requires Roslyn to compile the project's sources, so projects that cannot be compiled (e.g., certain project types or unsupported languages) are rejected.

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 dbf9771522)

Solutions

  1. Run precompilation against a standard compilable C# or VB project that contains the DbContext.
  2. Move the DbContext and queries into a compilable project if it currently lives in a non-compilable one.
  3. Skip '--precompile-queries' if you only need the compiled model ('--native-aot' scaffolding may still work without query precompilation depending on flags).
  4. Confirm the project's target framework and SDK are supported by the installed Roslyn/MSBuild.

Example fix

// before - optimizing a non-compilable project
dotnet ef dbcontext optimize --precompile-queries --project ./src/MyApp.Shared

// after - target a compilable application project
dotnet ef dbcontext optimize --precompile-queries --startup-project ./src/MyApp
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check: confirm the target project is compilable before optimizing.
var workspace = MSBuildWorkspace.Create();
var project = workspace.OpenProjectAsync(projectPath).GetAwaiter().GetResult();
if (!project.SupportsCompilation)
    throw new NotSupportedException($"Project '{projectPath}' does not support compilation; choose a compilable project.");

Try / catch

try { ops.Optimize(..., precompileQueries: true, ...); }
catch (NotSupportedException ex) when (ex.Message.Contains("does not support compilation"))
{
    // target a compilable C#/VB project instead
}

Prevention

When it happens

Trigger: Running the query-precompilation flow on a project that MSBuildWorkspace loaded but that does not support compilation. This includes some non-standard project kinds, projects without a compilable source set, or unsupported target frameworks/scenarios.

Common situations: Optimizing a project that is not a regular C#/VB compilable project (e.g., a shared MSBuild project, a project that only contains content). Using a project format or target that Roslyn's MSBuildWorkspace marks as non-compilable. Mixing languages where the loaded project is not compilable in the current workspace.

Related errors


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