{"record":{"id":"209a22f1e92a16f9","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-has-a-defining-quer","errorCode":null,"errorMessage":"The entity type '{entityType}' has a defining query configured. Compiled model can't be generated, because defining queries are not supported.","messagePattern":"The entity type '(.+?)' has a defining query configured\\. Compiled model can't be generated, because defining queries are not supported\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.InMemory/Design/Internal/InMemoryCSharpRuntimeAnnotationCodeGenerator.cs","lineNumber":40,"sourceCode":"    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public InMemoryCSharpRuntimeAnnotationCodeGenerator(\n        CSharpRuntimeAnnotationCodeGeneratorDependencies dependencies)\n        : base(dependencies)\n    {\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public override void Generate(IEntityType entityType, CSharpRuntimeAnnotationCodeGeneratorParameters parameters)\n    {\n        if (entityType.GetInMemoryQuery() != null)\n        {\n            throw new InvalidOperationException(InMemoryStrings.CompiledModelDefiningQuery(entityType.DisplayName()));\n        }\n\n        base.Generate(entityType, parameters);\n    }\n}\n","sourceCodeStart":22,"sourceCodeEnd":46,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.InMemory/Design/Internal/InMemoryCSharpRuntimeAnnotationCodeGenerator.cs#L22-L46","documentation":"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.","triggerScenarios":"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.","commonSituations":"Using entity.ToInMemoryQuery(...) or the older HasDefiningQuery on InMemory entities and then opting into the compiled-model optimization.","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."],"exampleFix":"// before\nmodelBuilder.Entity<Order>().ToInMemoryQuery(orders.Where(o => o.Active));\n// after\nmodelBuilder.Entity<Order>().HasQueryFilter(o => o.Active);\n// or remove entirely if compiled model is the priority","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n    if (et.GetInMemoryQuery() is not null)\n        throw new InvalidOperationException($\"Cannot use compiled model: {et.DisplayName()} has a defining query\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["in-memory","compiled-model","defining-query","design-time"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}