{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L338-L374","documentation":"Thrown by ValidateContainerIndexing when two document-root entity types in the same container disagree on whether automatic indexing is enabled. The validator treats an unconfigured (null) value as equivalent to true (Cosmos default), so an explicit HasAutomaticIndexing(false) on one root conflicts with an unconfigured or explicitly-true sibling.","triggerScenarios":"HasAutomaticIndexing(false) on one root entity sharing a container with another root that is unconfigured or HasAutomaticIndexing(true). Surfaces at model validation.","commonSituations":"Optimizing write-heavy workloads by disabling indexing on one entity while leaving siblings default; selective indexing migration where only part of the container was updated; one team's entity defaults to auto-indexing while another disabled it.","solutions":["Set HasAutomaticIndexing(false) (or true) consistently on every document-root entity in the shared container.","If you only want specific paths indexed, disable automatic indexing on all roots and declare explicit HasIndex calls.","Use a shared configuration method to apply the same indexing-enabled choice to every root in the container."],"exampleFix":"// before\nmodelBuilder.Entity<A>().ToContainer(\"shared\").HasAutomaticIndexing(false);\nmodelBuilder.Entity<B>().ToContainer(\"shared\"); // defaults to true\n\n// after\nmodelBuilder.Entity<A>().ToContainer(\"shared\").HasAutomaticIndexing(false);\nmodelBuilder.Entity<B>().ToContainer(\"shared\").HasAutomaticIndexing(false);","handlingStrategy":"validation","validationCode":"using var ctx = new MyContext();\nforeach (var grp in ctx.Model.GetEntityTypes()\n    .Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null && e.BaseType == null)\n    .GroupBy(e => e.GetContainer()))\n{\n    var enabledStates = grp.Select(e => e.GetAutomaticIndexingEnabled() ?? true).Distinct().ToList();\n    Debug.Assert(enabledStates.Count == 1, $\"Container {grp.Key} disagrees on automatic indexing enabled\");\n}","typeGuard":null,"tryCatchPattern":"try { ctx.Model.GetModel(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"automatic indexing is enabled for some\"))\n{\n    logger.LogError(ex, \"Automatic-indexing-enabled mismatch in shared container\");\n    throw;\n}","preventionTips":["Set HasAutomaticIndexing uniformly (true or false) on every root in a shared container.","Remember null defaults to enabled; make the choice explicit everywhere.","Group roots by container in a test and assert a single enabled value."],"tags":["cosmos","configuration","model-validation","indexing","shared-container"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}