dotnet/efcore · error · InvalidOperationException

The entity type '{entityType}' has a defining query configur

Error message

The entity type '{entityType}' has a defining query configured. Compiled model can't be generated, because defining queries are not supported.

What it means

Thrown by the InMemory provider's runtime-annotation code generator when generating a compiled model would require persisting a defining query (HasQueryFilter replacement via ToInMemoryQuery/HasDefiningQuery). Compiled models do not support defining queries, so the generator refuses rather than emit a model that silently drops the query.

Source

Thrown at src/EFCore.InMemory/Design/Internal/InMemoryCSharpRuntimeAnnotationCodeGenerator.cs:40

    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public InMemoryCSharpRuntimeAnnotationCodeGenerator(
        CSharpRuntimeAnnotationCodeGeneratorDependencies dependencies)
        : base(dependencies)
    {
    }

    /// <summary>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </summary>
    public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)
    {
        if (entityType.GetInMemoryQuery() != null)
        {
            throw new InvalidOperationException(InMemoryStrings.CompiledModelDefiningQuery(entityType.DisplayName()));
        }

        base.Generate(entityType, parameters);
    }
}

View on GitHub (pinned to 3a2006ef56)

Solutions

  1. Remove ToInMemoryQuery / HasDefiningQuery from the affected entity types if compiled model is required.
  2. Express the query filter logic as a regular LINQ Where on the DbSet or via a pre-filtered view in app code.
  3. Skip the compiled-model optimization for the InMemory context if defining queries are essential to tests.
  4. Switch the test database to SQLite in-memory (which supports query filters under compiled models) if the filter is required.

Example fix

// before
modelBuilder.Entity<Order>().ToInMemoryQuery(orders.Where(o => o.Active));
// after
modelBuilder.Entity<Order>().HasQueryFilter(o => o.Active);
// or remove entirely if compiled model is the priority
Defensive patterns

Strategy: validation

Validate before calling

foreach (var et in modelBuilder.Model.GetEntityTypes())
    if (et.GetInMemoryQuery() is not null)
        throw new InvalidOperationException($"Cannot use compiled model: {et.DisplayName()} has a defining query");

Prevention

When it happens

Trigger: Calling AddScaffolding / compiled-model generation (`dotnet ef dbcontext optimize` or DbContextOptions.UseModel) on an InMemory model where at least one IEntityType.GetInMemoryQuery() returns non-null.

Common situations: Using entity.ToInMemoryQuery(...) or the older HasDefiningQuery on InMemory entities and then opting into the compiled-model optimization.

Related errors


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