dotnet/efcore · error · InvalidOperationException

Cosmos automatic indexing is enabled for some entity types…

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 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.

Solutions

  1. Pick one value for HasAutomaticIndexing (true OR false) and apply it to every document-root entity in the named container.
  2. 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.
  3. If different entities need different indexing, map them to different containers.

Example fix

// before
modelBuilder.Entity<Invoice>().ToContainer("Docs").HasAutomaticIndexing(true);
modelBuilder.Entity<Receipt>().ToContainer("Docs").HasAutomaticIndexing(false);

// after
modelBuilder.Entity<Invoice>().ToContainer("Docs").HasAutomaticIndexing(false);
modelBuilder.Entity<Receipt>().ToContainer("Docs").HasAutomaticIndexing(false);
Defensive patterns

Strategy: validation

Validate before calling

foreach (var g in dbContext.Model.GetEntityTypes().GroupBy(e => e.GetContainer()))
{
    bool? enabled = null;
    foreach (var e in g.Where(et => et.BaseType is null))
    {
        var ann = e.FindAnnotation(CosmosAnnotationNames.AutomaticIndexingEnabled)?.Value as bool?;
        bool effective = ann ?? true;
        if (enabled is null) enabled = effective;
        else if (enabled != effective) throw new InvalidOperationException($"Indexing flag mismatch on {g.Key}");
    }
}

Prevention

When it happens

Trigger: 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.

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

Related errors


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

Appendix: 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 3a2006ef56)