{"record":{"id":"0b7db1910c681aad","repo":"dotnet/efcore","slug":"cosmos-automatic-indexing-is-enabled-for-some-enti","errorCode":null,"errorMessage":"Cosmos automatic indexing is enabled for some entity types but disabled for others on container '{container}'; entity types '{entityType1}' and '{entityType2}' disagree. All entity types mapped to the same container must agree on whether automatic indexing is enabled.","messagePattern":"Cosmos automatic indexing is enabled for some entity types but disabled for others on container '(.+?)'; entity types '(.+?)' and '(.+?)' disagree\\. All entity types mapped to the same container must agree on whether automatic indexing is enabled\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":356,"sourceCode":"                : 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;\n                }\n                else\n                {\n                    // Automatic indexing is enabled by default, so treat an unconfigured (null) value as equivalent to\n                    // an explicit 'true' when comparing across entity types in the same container.\n                    if ((currentEnabled ?? true) != (automaticIndexingEnabled ?? true))\n                    {\n                        throw new InvalidOperationException(\n                            CosmosStrings.InconsistentAutomaticIndexingEnabled(\n                                container,\n                                automaticIndexingOwner.DisplayName(),\n                                entityType.DisplayName()));\n                    }\n\n                    // Exceptions only affect the indexing policy when automatic indexing is enabled, so don't compare\n                    // them when it is disabled.\n                    if ((automaticIndexingEnabled ?? true)\n                        && ((currentExceptions is null) != (automaticIndexingExceptions is null)\n                            || (currentExceptions is not null\n                                && automaticIndexingExceptions is not null\n                                && !currentExceptions.SequenceEqual(automaticIndexingExceptions, StringComparer.Ordinal))))\n                    {\n                        throw new InvalidOperationException(\n                            CosmosStrings.InconsistentAutomaticIndexing(\n                                container,\n                                automaticIndexingOwner.DisplayName(),","sourceCodeStart":338,"sourceCodeEnd":374,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L338-L374","documentation":"Thrown when entity types in the same container disagree on whether automatic indexing is ENABLED. The validator tracks the first root entity's value (with null treated as true, since Cosmos defaults to indexing on) and throws if another root entity in the container has a conflicting enabled/disabled setting. Indexing policy is container-wide so all types must agree.","triggerScenarios":"Two document-root entity types share a container (e.g. via ToContainer) and one calls HasAutomaticIndexing(true) while the other calls HasAutomaticIndexing(false). The validator treats an unset value as true, so explicit false vs unset also collides. Fires during model validation.","commonSituations":"Multiple root entities mapped to the same container (a multi-root shared container) where indexing was configured on only some of them; flipping indexing off for one entity type without updating the others; teams independently configuring their entity types.","solutions":["Pick one value for HasAutomaticIndexing (true OR false) and apply it to every document-root entity in the named container.","Prefer configuring automatic indexing once on the primary root entity and leaving the others unconfigured, but note that an explicit false on any root will conflict with implicit true on the others - so be explicit and consistent.","If different entities need different indexing, map them to different containers."],"exampleFix":"// before\nmodelBuilder.Entity<Invoice>().ToContainer(\"Docs\").HasAutomaticIndexing(true);\nmodelBuilder.Entity<Receipt>().ToContainer(\"Docs\").HasAutomaticIndexing(false);\n\n// after\nmodelBuilder.Entity<Invoice>().ToContainer(\"Docs\").HasAutomaticIndexing(false);\nmodelBuilder.Entity<Receipt>().ToContainer(\"Docs\").HasAutomaticIndexing(false);","handlingStrategy":"validation","validationCode":"foreach (var g in dbContext.Model.GetEntityTypes().GroupBy(e => e.GetContainer()))\n{\n    bool? enabled = null;\n    foreach (var e in g.Where(et => et.BaseType is null))\n    {\n        var ann = e.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingEnabled)?.Value as bool?;\n        bool effective = ann ?? true;\n        if (enabled is null) enabled = effective;\n        else if (enabled != effective) throw new InvalidOperationException($\"Indexing flag mismatch on {g.Key}\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set HasAutomaticIndexing consistently on every document root of a shared container (remember null == true).","Avoid shared containers for entity types that need different indexing.","Validate the model in a unit test."],"tags":["cosmos","model-validation","indexing","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}