dotnet/efcore · error · InvalidOperationException

Cosmos automatic indexing is enabled for some entity types b

Error message

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.

What it means

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.

Source

Thrown at src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs:356

                : null;
            var currentExceptions = entityType.BaseType is null
                ? (IReadOnlyList<string>?)entityType.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingExceptions)?.Value
                : null;
            if (currentEnabled is not null || currentExceptions is not null)
            {
                if (automaticIndexingOwner is null)
                {
                    automaticIndexingOwner = entityType;
                    automaticIndexingEnabled = currentEnabled;
                    automaticIndexingExceptions = currentExceptions;
                }
                else
                {
                    // Automatic indexing is enabled by default, so treat an unconfigured (null) value as equivalent to
                    // an explicit 'true' when comparing across entity types in the same container.
                    if ((currentEnabled ?? true) != (automaticIndexingEnabled ?? true))
                    {
                        throw new InvalidOperationException(
                            CosmosStrings.InconsistentAutomaticIndexingEnabled(
                                container,
                                automaticIndexingOwner.DisplayName(),
                                entityType.DisplayName()));
                    }

                    // Exceptions only affect the indexing policy when automatic indexing is enabled, so don't compare
                    // them when it is disabled.
                    if ((automaticIndexingEnabled ?? true)
                        && ((currentExceptions is null) != (automaticIndexingExceptions is null)
                            || (currentExceptions is not null
                                && automaticIndexingExceptions is not null
                                && !currentExceptions.SequenceEqual(automaticIndexingExceptions, StringComparer.Ordinal))))
                    {
                        throw new InvalidOperationException(
                            CosmosStrings.InconsistentAutomaticIndexing(
                                container,
                                automaticIndexingOwner.DisplayName(),

View on GitHub (pinned to dbf9771522)

Solutions

  1. Set HasAutomaticIndexing(false) (or true) consistently on every document-root entity in the shared container.
  2. If you only want specific paths indexed, disable automatic indexing on all roots and declare explicit HasIndex calls.
  3. Use a shared configuration method to apply the same indexing-enabled choice to every root in the container.

Example fix

// before
modelBuilder.Entity<A>().ToContainer("shared").HasAutomaticIndexing(false);
modelBuilder.Entity<B>().ToContainer("shared"); // defaults to true

// after
modelBuilder.Entity<A>().ToContainer("shared").HasAutomaticIndexing(false);
modelBuilder.Entity<B>().ToContainer("shared").HasAutomaticIndexing(false);
Defensive patterns

Strategy: validation

Validate before calling

using var ctx = new MyContext();
foreach (var grp in ctx.Model.GetEntityTypes()
    .Where(e => e.FindPrimaryKey() != null && e.GetContainer() != null && e.BaseType == null)
    .GroupBy(e => e.GetContainer()))
{
    var enabledStates = grp.Select(e => e.GetAutomaticIndexingEnabled() ?? true).Distinct().ToList();
    Debug.Assert(enabledStates.Count == 1, $"Container {grp.Key} disagrees on automatic indexing enabled");
}

Try / catch

try { ctx.Model.GetModel(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("automatic indexing is enabled for some"))
{
    logger.LogError(ex, "Automatic-indexing-enabled mismatch in shared container");
    throw;
}

Prevention

When it happens

Trigger: HasAutomaticIndexing(false) on one root entity sharing a container with another root that is unconfigured or HasAutomaticIndexing(true). Surfaces at model validation.

Common situations: 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.

Related errors


AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06). Data as JSON: /api/errors/0b7db1910c681aad. Report an issue: GitHub.