{"record":{"id":"f70bf47d9deb3249","repo":"dotnet/efcore","slug":"cosmos-automatic-indexing-configuration-was-set-on","errorCode":null,"errorMessage":"Cosmos automatic-indexing configuration was set on entity type '{entityType}', but it must be configured on the document-root entity type '{rootEntityType}' instead. Automatic-indexing settings apply to the container as a whole and are inherited by derived types.","messagePattern":"Cosmos automatic-indexing configuration was set on entity type '(.+?)', but it must be configured on the document-root entity type '(.+?)' instead\\. Automatic-indexing settings apply to the container as a whole and are inherited by derived types\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":330,"sourceCode":"\n        ValidateContainerIndexing(mappedTypes, container);\n    }\n\n    private static void ValidateContainerIndexing(IReadOnlyList<IEntityType> mappedTypes, string container)\n    {\n        IEntityType? automaticIndexingOwner = null;\n        bool? automaticIndexingEnabled = null;\n        IReadOnlyList<string>? automaticIndexingExceptions = null;\n\n        foreach (var entityType in mappedTypes)\n        {\n            // Only document-root entity types can carry automatic-indexing configuration.\n            // The same setting must apply to every entity type in a shared container.\n            if (entityType.BaseType is not null\n                && (entityType.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingEnabled) is not null\n                    || entityType.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingExceptions) is not null))\n            {\n                throw new InvalidOperationException(\n                    CosmosStrings.AutomaticIndexingNotOnRoot(\n                        entityType.DisplayName(),\n                        entityType.GetRootType().DisplayName()));\n            }\n\n            var currentEnabled = entityType.BaseType is null\n                ? (bool?)entityType.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingEnabled)?.Value\n                : null;\n            var currentExceptions = entityType.BaseType is null\n                ? (IReadOnlyList<string>?)entityType.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingExceptions)?.Value\n                : null;\n            if (currentEnabled is not null || currentExceptions is not null)\n            {\n                if (automaticIndexingOwner is null)\n                {\n                    automaticIndexingOwner = entityType;\n                    automaticIndexingEnabled = currentEnabled;\n                    automaticIndexingExceptions = currentExceptions;","sourceCodeStart":312,"sourceCodeEnd":348,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L312-L348","documentation":"Thrown when an entity type that is NOT a document root (it has a BaseType / is a derived type in the hierarchy) carries an AutomaticIndexingEnabled or AutomaticIndexingExceptions annotation. Cosmos indexing policy is a container-level concern owned by the document root; derived types inherit it and cannot override it locally.","triggerScenarios":"A derived entity type (one with a BaseType) calls HasAutomaticIndexing(true/false) or HasAutomaticIndexingExceptions(...). The validator checks FindAnnotation for either name on any non-root type and throws.","commonSituations":"Applying IEntityTypeConfiguration<T> uniformly to every type in a hierarchy including derived ones; copying a root-type config to a derived type; treating automatic indexing as a per-type setting when it is actually per-container.","solutions":["Move the HasAutomaticIndexing / HasAutomaticIndexingExceptions call from the derived type to the document-root entity type.","If you use IEntityTypeConfiguration<T>, guard the indexing calls with a check that the type is the root (or only register the configuration for the root).","Search the codebase for HasAutomaticIndexing and verify every call site targets a root entity."],"exampleFix":"// before\nmodelBuilder.Entity<SpecialOrder>()   // derives from Order\n    .HasAutomaticIndexing(false);\n\n// after\nmodelBuilder.Entity<Order>()           // the document root\n    .HasAutomaticIndexing(false);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"// Guard: only call HasAutomaticIndexing when configuring a document root.\nstatic bool IsDocumentRoot(Type t, IModel model)\n    => model.FindEntityType(t)?.IsDocumentRoot() ?? false;\n\n// usage in OnModelCreating\nif (IsDocumentRoot(typeof(Order), modelBuilder.Model))\n    modelBuilder.Entity<Order>().HasAutomaticIndexing(false);","tryCatchPattern":null,"preventionTips":["Only configure automatic indexing on document-root entity types.","Register IEntityTypeConfiguration<T> only for root types when the config includes indexing settings.","Add a CI test that asserts no derived type carries the AutomaticIndexing annotations."],"tags":["cosmos","model-validation","indexing","hierarchy","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}