dotnet/efcore · error · InvalidOperationException

Compilation failed with errors:

Error message

Compilation failed with errors:

What it means

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.

Source

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

        }

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

        var precompiledQueryCodeGenerator = services.GetRequiredService<IPrecompiledQueryCodeGeneratorSelector>().Select(_language);

        var precompilationErrors = new List<PrecompiledQueryCodeGenerator.QueryPrecompilationError>();
        var generatedFiles = precompiledQueryCodeGenerator.GeneratePrecompiledQueries(
            compilation,
            syntaxGenerator,
            context,
            memberAccessReplacements,
            precompilationErrors,
            generatedFileNames,
            assembly: _assembly,
            suffix);

View on GitHub (pinned to dbf9771522)

Solutions

  1. Build the project first ('dotnet build') and fix every compile error before running optimize.
  2. Restore packages ('dotnet restore') so referenced assemblies are present.
  3. Ensure the same configuration used at runtime is used at design time (e.g., define constants, target framework).
  4. Read the listed diagnostics in the error message to locate and fix each CS error.

Example fix

# before - project has compile errors
dotnet ef dbcontext optimize --precompile-queries  # lists 'Compilation failed with errors:'

# after - build clean first, then optimize
dotnet build
dotnet ef dbcontext optimize --precompile-queries
Defensive patterns

Strategy: validation

Try / catch

try { ops.Optimize(..., precompileQueries: true, ...); }
catch (InvalidOperationException ex) when (ex.Message.Contains("Compilation failed with errors"))
{
    // read the listed CS diagnostics, fix each compile error, rebuild, then retry
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


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