{"record":{"id":"271abf3da71026d4","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-cannot-have-a-defin","errorCode":null,"errorMessage":"The entity type '{entityType}' cannot have a defining query because it is derived from '{baseType}'. Only base entity types can have a defining query.","messagePattern":"The entity type '(.+?)' cannot have a defining query because it is derived from '(.+?)'\\. Only base entity types can have a defining query\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.InMemory/Infrastructure/Internal/InMemoryModelValidator.cs","lineNumber":53,"sourceCode":"        ValidateDefiningQuery(model, logger);\n    }\n\n    /// <summary>\n    ///     Validates the configuration of defining queries in the model.\n    /// </summary>\n    /// <param name=\"model\">The model to validate.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected virtual void ValidateDefiningQuery(\n        IModel model,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        foreach (var entityType in model.GetEntityTypes())\n        {\n            if (entityType.GetInMemoryQuery() != null)\n            {\n                if (entityType.BaseType != null)\n                {\n                    throw new InvalidOperationException(\n                        CoreStrings.DerivedTypeDefiningQuery(entityType.DisplayName(), entityType.BaseType.DisplayName()));\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.InMemory/Infrastructure/Internal/InMemoryModelValidator.cs#L35-L60","documentation":"Thrown by the InMemory model validator when a derived entity type (one with a BaseType) has an InMemory defining query configured. EF Core restricts defining queries to base entity types because derived types inherit the query from their parent; configuring one on a derived type is ambiguous and invalid.","triggerScenarios":"ValidateDefiningQuery iterates model.GetEntityTypes(); an entity with GetInMemoryQuery() != null also has entityType.BaseType != null at validation time.","commonSituations":"TPH/TPT hierarchies where the developer calls ToInMemoryQuery on a derived entity instead of the root. Migrating from HasQueryFilter to ToInMemoryQuery and applying it to the wrong end of a hierarchy.","solutions":["Move the ToInMemoryQuery / HasDefiningQuery call to the root (base) entity type of the hierarchy.","If only the derived set should be filtered, filter the source query inside the root entity's defining query using OfType<Derived>.","Remove the defining query from the derived type and use a discriminator-aware query filter instead."],"exampleFix":"// before - on derived type\nmodelBuilder.Entity<SpecialOrder>().ToInMemoryQuery(...);\n// after - on root type\nmodelBuilder.Entity<Order>().ToInMemoryQuery(orders.Where(o => o is SpecialOrder).Cast<SpecialOrder>());","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n    if (et.BaseType is not null && et.GetInMemoryQuery() is not null)\n        throw new InvalidOperationException($\"Derived type {et.DisplayName()} cannot have a defining query\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply ToInMemoryQuery only to root entity types.","Filter derived rows inside the root defining query using OfType<T>.","Review hierarchy configuration when adding defining queries."],"tags":["in-memory","validation","defining-query","inheritance"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}