dotnet/efcore · error · InvalidOperationException

Query precompilation failed with errors:

Error message

Query precompilation failed with errors:

What it means

InvalidOperationException from DbContextOperations when precompilation itself produced errors (the precompilationErrors list is non-empty after GeneratePrecompiledQueries ran). Unlike project-load or compile errors, these come from the query-precompilation pipeline failing to translate/emit individual queries.

Solutions

  1. Inspect each error in the thrown message — they identify the specific query/expression that failed.
  2. Simplify or rewrite the offending LINQ query to a shape the precompiler supports.
  3. Update the EF Core and provider packages — precompiled query coverage is still maturing.
  4. If a specific query cannot be precompiled, exclude that path from precompilation or disable --precompile-queries and file an issue with the repro.

Example fix

// before - unsupported query operator trips the precompiler
var q = db.Set<Order>().Select(o => ComplexTransformation(o));

// after - move the unsupported transform out of the translated LINQ
var raw = db.Set<Order>().AsEnumerable();
var q = raw.Select(o => ComplexTransformation(o));
Defensive patterns

Strategy: try-catch

Validate before calling

// run optimize with --no-precompile-queries first to confirm the model compiles
operations.Optimize(outDir, ..., precompileQueries: false, ...);
// only then enable precompilation

Try / catch

try { operations.Optimize(..., precompileQueries: true, ...); }
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Query precompilation failed"))
{ /* inspect each error; simplify the offending query; update provider */ }

Prevention

When it happens

Trigger: `dotnet ef dbcontext optimize --precompile-queries` completes project load and compilation but the precompiled-query generator reports errors while processing LINQ queries in the model (unsupported translation, missing service, AOT-incompatible query).

Common situations: Queries that use runtime-only features incompatible with precompilation/AOT, a provider that does not fully support precompiled queries, or a bug in the precompiled query code generator for a specific query shape.

Related errors


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

Appendix: source

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

            compilation,
            syntaxGenerator,
            context,
            memberAccessReplacements,
            precompilationErrors,
            generatedFileNames,
            assembly: _assembly,
            suffix);

        if (precompilationErrors.Count > 0)
        {
            var errorBuilder = new StringBuilder();
            errorBuilder.AppendLine(DesignStrings.QueryPrecompilationErrors);
            foreach (var error in precompilationErrors)
            {
                errorBuilder.AppendLine(error.ToString());
            }

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

        var writtenFiles = new List<string>();
        return CompiledModelScaffolder.WriteFiles(generatedFiles, outputDir);
    }

    private string? GetNamespaceFromOutputPath(string directoryPath)
    {
        var subNamespace = SubnamespaceFromOutputPath(_projectDir, directoryPath);
        return string.IsNullOrEmpty(subNamespace)
            ? _rootNamespace
            : string.IsNullOrEmpty(_rootNamespace)
                ? subNamespace
                : _rootNamespace + "." + subNamespace;
    }

    // if outputDir is a subfolder of projectDir, then use each subfolder as a sub-namespace
    // --output-dir $(projectFolder)/A/B/C

View on GitHub (pinned to 3a2006ef56)