dotnet/efcore · error · InvalidOperationException
The container with the name '{containerName}' does not exist
Error message
The container with the name '{containerName}' does not exist. What it means
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.
Source
Thrown at src/EFCore.Cosmos/Storage/Internal/SessionTokenStorage.cs:53
_containerSessionTokens = containerNames.ToDictionary(x => x, x => new CompositeSessionToken(_defaultToken));
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void SetSessionTokens(IReadOnlyDictionary<string, string?> sessionTokens)
{
CheckMode();
foreach (var sessionToken in sessionTokens)
{
ref var containerSessionToken = ref CollectionsMarshal.GetValueRefOrNullRef(_containerSessionTokens, sessionToken.Key);
if (Unsafe.IsNullRef(ref containerSessionToken))
{
throw new InvalidOperationException(CosmosStrings.ContainerNameDoesNotExist(sessionToken.Key));
}
containerSessionToken = new CompositeSessionToken(sessionToken.Value, true);
}
}
/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public virtual void AppendSessionTokens(IReadOnlyDictionary<string, string> sessionTokens)
{
CheckMode();
foreach (var sessionToken in sessionTokens)
{
ref var containerSessionToken = ref CollectionsMarshal.GetValueRefOrNullRef(_containerSessionTokens, sessionToken.Key);View on GitHub (pinned to dbf9771522)
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.
Example fix
// before
db.GetService<ISessionTokenStorage>().SetSessionTokens(new Dictionary<string,string?>
{
["Orders"] = token1,
["Custmers"] = token2 // typo -> ContainerNameDoesNotExist
});
// after
var known = db.GetService<IModel>().GetEntityTypes().Select(t => t.GetContainer()).ToHashSet();
var safe = incoming.Where(kv => known.Contains(kv.Key)).ToDictionary();
db.GetService<ISessionTokenStorage>().SetSessionTokens(safe); Defensive patterns
Strategy: validation
Validate before calling
// Validate container names against the model before setting session tokens
var known = db.GetService<IModel>()
.GetEntityTypes()
.Select(t => t.GetContainer())
.Where(n => n is not null)
.ToHashSet(StringComparer.Ordinal);
var safe = incoming.Where(kv => known.Contains(kv.Key)).ToDictionary(kv => kv.Key, kv => kv.Value);
db.GetService<ISessionTokenStorage>().SetSessionTokens(safe); Type guard
static bool IsKnownContainer(DbContext db, string containerName)
{
return db.GetService<IModel>().GetEntityTypes()
.Select(t => t.GetContainer())
.Contains(containerName);
} Try / catch
try { db.GetService<ISessionTokenStorage>().SetSessionTokens(tokens); }
catch (InvalidOperationException ex) when (ex.Message.Contains("does not exist"))
{
// log the unknown container name from the message, filter the dictionary, and retry
var known = db.GetService<IModel>().GetEntityTypes().Select(t => t.GetContainer()).ToHashSet();
var safe = tokens.Where(kv => known.Contains(kv.Key)).ToDictionary();
db.GetService<ISessionTokenStorage>().SetSessionTokens(safe);
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Cosmos-specific methods can only be used when the context is
- No session token has been set for container: '{container}'.
- Disable automatic session token management using 'options.Se
- The value '{value}' provided for argument '{argumentName}' m
- An Azure Cosmos DB container name is defined on entity type
AI-assisted analysis of dotnet/efcore@dbf9771522 (2026-08-06).
Data as JSON: /api/errors/02a492d559be7f54.
Report an issue: GitHub.