{"record":{"id":"c3bee2a2cb4d02c5","repo":"dotnet/efcore","slug":"except-cannot-be-called-on-the-builder-returned","errorCode":null,"errorMessage":"'Except' cannot be called on the builder returned by 'HasAutomaticIndexing(false)' because exceptions only apply when automatic indexing is enabled.","messagePattern":"'Except' cannot be called on the builder returned by 'HasAutomaticIndexing\\(false\\)' because exceptions only apply when automatic indexing is enabled\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Metadata/Builders/CosmosAutomaticIndexingBuilder.cs","lineNumber":57,"sourceCode":"\n    /// <summary>\n    ///     Adds a path to the container's <c>ExcludedPaths</c>. The path must use Cosmos indexing-policy syntax\n    ///     (e.g., <c>/secret/?</c> for a leaf, <c>/items/[]/*</c> for an array sub-tree). Throws if automatic\n    ///     indexing was disabled via <c>HasAutomaticIndexing(false)</c>.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://learn.microsoft.com/azure/cosmos-db/index-policy\">Indexing policies in Azure Cosmos DB</see>\n    ///     for more information.\n    /// </remarks>\n    /// <param name=\"path\">The path to exclude from indexing.</param>\n    /// <returns>The same builder instance so that multiple calls can be chained.</returns>\n    public virtual CosmosAutomaticIndexingBuilder Except(string path)\n    {\n        Check.NotEmpty(path);\n\n        if (EntityTypeBuilder.Metadata.GetAutomaticIndexingEnabled() == false)\n        {\n            throw new InvalidOperationException(CosmosStrings.AutomaticIndexingExceptionWhileDisabled);\n        }\n\n        var current = EntityTypeBuilder.Metadata.GetAutomaticIndexingExceptions();\n        var updated = new List<string>((current?.Count ?? 0) + 1);\n        if (current is not null)\n        {\n            updated.AddRange(current);\n        }\n\n        updated.Add(path);\n        EntityTypeBuilder.Metadata.SetAutomaticIndexingExceptions(updated);\n\n        return this;\n    }\n}\n\n/// <summary>\n///     A generic fluent builder used to configure the Cosmos container's automatic indexing policy. Returned by","sourceCodeStart":39,"sourceCodeEnd":75,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Metadata/Builders/CosmosAutomaticIndexingBuilder.cs#L39-L75","documentation":"Thrown by CosmosAutomaticIndexingBuilder.Except when EntityTypeBuilder.Metadata.GetAutomaticIndexingEnabled() == false. Excluded paths (Cosmos indexing policy ExcludedPaths) only have meaning when automatic indexing is on; calling Except after HasAutomaticIndexing(false) is contradictory, so EF rejects it.","triggerScenarios":"Chaining .HasAutomaticIndexing(false).Except(\"/secret/?\") on an EntityTypeBuilder, or calling Except after a separate call that disabled automatic indexing on the same entity.","commonSituations":"Copy-pasting indexing configuration from a 'with exceptions' snippet onto an entity whose indexing was already disabled. Toggling HasAutomaticIndexing off during troubleshooting but leaving Except calls in place.","solutions":["Remove the Except calls: if automatic indexing is disabled, nothing is indexed so exclusions are meaningless.","If you actually want indexing on but with exclusions, switch to HasAutomaticIndexing(true) (or omit the flag) and keep the Except calls."],"exampleFix":"// before\nmodelBuilder.Entity<Doc>()\n    .HasAutomaticIndexing(false)\n    .Except(\"/secret/?\");\n\n// after (option A: keep indexing off, drop exclusions)\nmodelBuilder.Entity<Doc>().HasAutomaticIndexing(false);\n// after (option B: keep exclusions, enable indexing)\nmodelBuilder.Entity<Doc>()\n    .HasAutomaticIndexing(true)\n    .Except(\"/secret/?\");","handlingStrategy":"validation","validationCode":"// Before calling Except, confirm automatic indexing is not disabled\nif (entityType.GetAutomaticIndexingEnabled() == false)\n{\n    throw new InvalidOperationException(\"Cannot call Except: automatic indexing is disabled.\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never chain Except after HasAutomaticIndexing(false).","Keep indexing configuration for each entity in one contiguous block so disabled-mode and Except calls are visually obvious if conflicting."],"tags":["cosmos","ef-core","metadata","indexing"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}