{"record":{"id":"3a6d5565d7fda598","repo":"dotnet/efcore","slug":"no-session-token-has-been-set-for-container-con","errorCode":null,"errorMessage":"No session token has been set for container: '{container}'. While using 'EnforceManual' mode you must always set a session token for any container used on every context instance.","messagePattern":"No session token has been set for container: '(.+?)'\\. While using 'EnforceManual' mode you must always set a session token for any container used on every context instance\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs","lineNumber":166,"sourceCode":"    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual string? GetSessionToken(string containerName)\n    {\n        ArgumentException.ThrowIfNullOrWhiteSpace(containerName);\n\n        if (_mode == SessionTokenManagementMode.FullyAutomatic)\n        {\n            return null;\n        }\n\n        var sessionToken = _containerSessionTokens[containerName];\n\n        if (!sessionToken.IsSet)\n        {\n            if (_mode == SessionTokenManagementMode.EnforcedManual)\n            {\n                throw new InvalidOperationException(CosmosStrings.MissingSessionTokenEnforceManual(containerName));\n            }\n\n            if (_mode == SessionTokenManagementMode.SemiAutomatic)\n            {\n                return null;\n            }\n        }\n\n        return sessionToken.ConvertToString();\n    }\n\n    /// <summary>\n    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    public virtual void TrackSessionToken(string containerName, string? sessionToken)","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs#L148-L184","documentation":"Thrown by SessionTokenStorage.GetSessionToken when operating in SessionTokenManagementMode.EnforcedManual and the requested container has no session token set (IsSet is false). EnforcedManual is intentionally strict: every container touched by a context instance must have an explicitly-provided session token on every instance, so omitting one is treated as a programming error rather than degraded behavior.","triggerScenarios":"Selecting SessionTokenManagementMode.EnforcedManual in Cosmos options and then executing a query or save against a container whose session token was never set via SetSessionTokens/TrackSessionToken. The check fires inside GetSessionToken right before the token would be returned to the Cosmos SDK.","commonSituations":"Switching from FullyAutomatic or SemiAutomatic to EnforcedManual to enforce consistency guarantees, but forgetting to seed a session token for one or more containers. Adding a new entity/container to the model after enabling EnforcedManual without updating the token-seeding code. Multi-container contexts where only the primary container's token is set.","solutions":["Call SetSessionTokens (or TrackSessionToken) for every container the context will use before issuing any operation, in EnforcedManual mode.","If you do not need strict enforcement, downgrade to SemiAutomatic (returns null instead of throwing) or FullyAutomatic.","Enumerate the model's containers (GetTrackedTokens keys) at startup and assert a token is provided for each."],"exampleFix":"// before - EnforcedManual, token missing for 'Orders'\noptionsBuilder.UseCosmos(..., o => o.SessionTokenManagementMode(SessionTokenManagementMode.EnforcedManual));\ncontext.Orders.Add(entity); // throws: no token for 'Orders'\n\n// after - seed tokens for all containers first\nstorage.SetSessionTokens(new Dictionary<string,string?> {\n    [\"Orders\"] = capturedToken,\n    [\"Customers\"] = capturedToken2\n});\ncontext.SaveChanges();","handlingStrategy":"validation","validationCode":"// In EnforcedManual mode, assert every container has a token before use.\nvar tracked = sessionTokenStorage.GetTrackedTokens();\nvar missing = tracked.Where(kv => string.IsNullOrEmpty(kv.Value)).Select(kv => kv.Key).ToList();\nif (missing.Count != 0)\n    throw new InvalidOperationException($\"Missing session tokens for: {string.Join(\", \", missing)}\");","typeGuard":null,"tryCatchPattern":"try { context.SaveChanges(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"No session token has been set\"))\n{\n    // capture and set the token for the named container, then retry\n}","preventionTips":["Seed session tokens for all containers at context construction in EnforcedManual mode.","Use SemiAutomatic during development to surface missing tokens as nulls instead of throws.","Automate token capture from a warm-up query and distribute to all context instances."],"tags":["cosmos","session-token","consistency","configuration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}