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
- Remove ToInMemoryQuery / HasDefiningQuery from the affected entity types if compiled model is required.
- Express the query filter logic as a regular LINQ Where on the DbSet or via a pre-filtered view in app code.
- Skip the compiled-model optimization for the InMemory context if defining queries are essential to tests.
- 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
- Do not combine ToInMemoryQuery/HasDefiningQuery with `dotnet ef dbcontext optimize`.
- Prefer HasQueryFilter for filters that must survive compiled-model generation.
- Document in tests when defining queries are intentional.
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
- The entity type '{entityType}' cannot have a defining query
- The function '{function}' has a custom translation. A compil
- The literal expression '{expression}' for '{type}' cannot be
- Cannot scaffold C# literals of type '{literalType}'. The pro
- A type-qualified method call requires an instance identifier
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/209a22f1e92a16f9.
Report an issue: GitHub.