{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Metadata/Builders/CosmosAutomaticIndexingBuilder.cs#L39-L75","documentation":"Thrown by CosmosAutomaticIndexingBuilder.Except when EntityTypeBuilder.Metadata.GetAutomaticIndexingEnabled() == false. Path exceptions only have meaning while automatic indexing is on, so calling Except() on a builder produced by HasAutomaticIndexing(false) is rejected immediately.","triggerScenarios":"Calling modelBuilder.Entity<T>().HasAutomaticIndexing(false).Except(\"/some/path\") in one chain.","commonSituations":"Turning off automatic indexing and then trying to carve out exception paths in the same fluent chain; copy-pasting indexing-policy code from an enabled configuration into a disabled one.","solutions":["Remove the .Except(...) call when HasAutomaticIndexing(false) is set (with auto-indexing off, paths are excluded by default).","If you want selective indexing, call HasAutomaticIndexing(true) and then chain .Except(path) for each path to exclude.","Re-read your indexing policy: when auto-indexing is off you opt paths IN, not OUT."],"exampleFix":"// before\nmodelBuilder.Entity<Doc>()\n    .HasAutomaticIndexing(false)\n    .Except(\"/temp/*\");\n\n// after - choose one\n// (a) auto-indexing off: drop Except\nmodelBuilder.Entity<Doc>().HasAutomaticIndexing(false);\n// (b) auto-indexing on with exceptions\nmodelBuilder.Entity<Doc>()\n    .HasAutomaticIndexing(true)\n    .Except(\"/temp/*\");","handlingStrategy":"type-guard","validationCode":"// Before chaining Except, check the current setting.\nvar meta = modelBuilder.Entity<Doc>().Metadata;\nif (meta.GetAutomaticIndexingEnabled() == false)\n    throw new InvalidOperationException(\"Cannot call Except() when automatic indexing is disabled.\");","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remember the indexing direction: when auto-indexing is OFF you add included paths; when ON you add excluded paths via Except.","Keep the indexing-policy chain together and read it as a unit.","Add a fluent helper that picks Except vs IncludedPath based on the enabled flag."],"tags":["cosmos","indexing","configuration","fluent-api"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}