{"record":{"id":"02a492d559be7f54","repo":"dotnet/efcore","slug":"the-container-with-the-name-containername-does","errorCode":null,"errorMessage":"The container with the name '{containerName}' does not exist.","messagePattern":"The container with the name '(.+?)' does not exist\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs","lineNumber":53,"sourceCode":"\n        _containerSessionTokens = containerNames.ToDictionary(x => x, x => new CompositeSessionToken(_defaultToken));\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 SetSessionTokens(IReadOnlyDictionary<string, string?> sessionTokens)\n    {\n        CheckMode();\n        foreach (var sessionToken in sessionTokens)\n        {\n            ref var containerSessionToken = ref CollectionsMarshal.GetValueRefOrNullRef(_containerSessionTokens, sessionToken.Key);\n            if (Unsafe.IsNullRef(ref containerSessionToken))\n            {\n                throw new InvalidOperationException(CosmosStrings.ContainerNameDoesNotExist(sessionToken.Key));\n            }\n\n            containerSessionToken = new CompositeSessionToken(sessionToken.Value, true);\n        }\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 AppendSessionTokens(IReadOnlyDictionary<string, string> sessionTokens)\n    {\n        CheckMode();\n        foreach (var sessionToken in sessionTokens)\n        {\n            ref var containerSessionToken = ref CollectionsMarshal.GetValueRefOrNullRef(_containerSessionTokens, sessionToken.Key);","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs#L35-L71","documentation":"Thrown by SessionTokenStorage.SetSessionTokens when a container name in the supplied session-token dictionary is not in the set of container names the storage was constructed with (the configured Cosmos containers). Session tokens are managed per-container, so providing a token for an unknown container name is rejected as a configuration/usage error (CosmosStrings.ContainerNameDoesNotExist). Only relevant when manual session-token management mode is enabled.","triggerScenarios":"Calling CosmosExtensions/manual session-token APIs to SetSessionTokens with a dictionary whose key does not match any container name registered in the model (modelBuilder.HasContainer / ToContainer). A typo in the container name, or a name from a different environment, triggers it.","commonSituations":"Typo in container name passed to SetSessionTokens; copying session tokens from another environment/database whose containers differ; renaming a container in the model without updating the token-sync code; enabling manual session-token management and passing stale token maps.","solutions":["Use the exact container name configured in the model (check ToContainer calls / HasContainer).","Filter the incoming token dictionary to only known container names before calling SetSessionTokens.","Re-derive the known container names from the model (IModel.GetEntityTypes().Select(t => t.GetContainer())) and validate against them.","Switch to FullyAutomatic session-token mode if you do not need manual control."],"exampleFix":"// before\ndb.GetService<ISessionTokenStorage>().SetSessionTokens(new Dictionary<string,string?>\n{\n    [\"Orders\"] = token1,\n    [\"Custmers\"] = token2 // typo -> ContainerNameDoesNotExist\n});\n\n// after\nvar known = db.GetService<IModel>().GetEntityTypes().Select(t => t.GetContainer()).ToHashSet();\nvar safe = incoming.Where(kv => known.Contains(kv.Key)).ToDictionary();\ndb.GetService<ISessionTokenStorage>().SetSessionTokens(safe);","handlingStrategy":"validation","validationCode":"// Validate container names against the model before setting session tokens\nvar known = db.GetService<IModel>()\n    .GetEntityTypes()\n    .Select(t => t.GetContainer())\n    .Where(n => n is not null)\n    .ToHashSet(StringComparer.Ordinal);\nvar safe = incoming.Where(kv => known.Contains(kv.Key)).ToDictionary(kv => kv.Key, kv => kv.Value);\ndb.GetService<ISessionTokenStorage>().SetSessionTokens(safe);","typeGuard":"static bool IsKnownContainer(DbContext db, string containerName)\n{\n    return db.GetService<IModel>().GetEntityTypes()\n        .Select(t => t.GetContainer())\n        .Contains(containerName);\n}","tryCatchPattern":"try { db.GetService<ISessionTokenStorage>().SetSessionTokens(tokens); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not exist\"))\n{\n    // log the unknown container name from the message, filter the dictionary, and retry\n    var known = db.GetService<IModel>().GetEntityTypes().Select(t => t.GetContainer()).ToHashSet();\n    var safe = tokens.Where(kv => known.Contains(kv.Key)).ToDictionary();\n    db.GetService<ISessionTokenStorage>().SetSessionTokens(safe);\n}","preventionTips":["Always derive container names from the model rather than hard-coding strings.","Filter incoming token maps to known containers at the boundary.","Use FullyAutomatic session-token mode unless you specifically need manual control.","Re-audit token-sync code after any container rename."],"tags":["cosmos","session-token","container","configuration","efcore"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}