{"record":{"id":"8ed5c0c012d9c84b","repo":"dotnet/efcore","slug":"cosmos-specific-methods-can-only-be-used-when-the","errorCode":null,"errorMessage":"Cosmos-specific methods can only be used when the context is using the Cosmos provider.","messagePattern":"Cosmos-specific methods can only be used when the context is using the Cosmos provider\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Extensions/CosmosDatabaseFacadeExtensions.cs","lineNumber":96,"sourceCode":"    /// <summary>\n    ///     Appends the composite sessions token per container for this <see cref=\"DbContext\" /> with the tokens specified in\n    ///     <paramref name=\"sessionTokens\" />.\n    /// </summary>\n    /// <remarks>See https://aka.ms/efcore-docs-cosmos-session for more information.</remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>\n    /// <param name=\"sessionTokens\">The session tokens to append per container.</param>\n    public static void AppendSessionTokens(this DatabaseFacade databaseFacade, IReadOnlyDictionary<string, string> sessionTokens)\n    {\n        var sessionTokenStorage = GetSessionTokenStorage(databaseFacade);\n\n        sessionTokenStorage.AppendSessionTokens(sessionTokens);\n    }\n\n    private static ISessionTokenStorage GetSessionTokenStorage(DatabaseFacade databaseFacade)\n    {\n        var db = GetService<IDatabase>(databaseFacade);\n        return db is not CosmosDatabaseWrapper dbWrapper\n            ? throw new InvalidOperationException(CosmosStrings.CosmosNotInUse)\n            : dbWrapper.SessionTokenStorage;\n    }\n\n    private static TService GetService<TService>(IInfrastructure<IServiceProvider> databaseFacade)\n        where TService : class\n    {\n        var service = databaseFacade.GetService<TService>();\n        return service ?? throw new InvalidOperationException(CosmosStrings.CosmosNotInUse);\n    }\n\n    /// <summary>\n    ///     Gets the configured database name for this <see cref=\"DbContext\" />.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-cosmos\">Accessing Azure Cosmos DB with EF Core</see> for more information and examples.\n    /// </remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>\n    /// <returns>The database name.</returns>","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Extensions/CosmosDatabaseFacadeExtensions.cs#L78-L114","documentation":"The GetSessionTokenStorage helper in CosmosDatabaseFacadeExtensions resolves the ISessionTokenStorage from the Cosmos provider's CosmosDatabaseWrapper. It throws InvalidOperationException(CosmosNotInUse) when the registered IDatabase service is not a CosmosDatabaseWrapper, meaning the DbContext is not configured with the Cosmos provider. This guard protects Cosmos-specific session-token APIs from being called on a non-Cosmos context.","triggerScenarios":"Calling databaseFacade.AppendSessionTokens(tokens) or GetSessionTokenStorage-related methods on a DbContext that uses SQL Server, PostgreSQL, SQLite, or in-memory provider instead of Cosmos. The IDatabase service resolved from the DI container is not a CosmosDatabaseWrapper.","commonSituations":"Switching providers in a multi-environment setup where dev uses Cosmos but tests use SQLite/InMemory; calling session-token APIs from shared infrastructure code without checking the provider; misconfigured DbContext that is missing the UseCosmos() call in OnConfiguring or AddDbContext.","solutions":["Ensure the DbContext is configured with optionsBuilder.UseCosmos(...) before calling session-token extension methods.","Guard Cosmos-specific calls with databaseFacade.IsCosmos() before invoking them.","Separate Cosmos-specific code paths from generic infrastructure code using provider checks."],"exampleFix":"// before — called without provider guard on any context\ncontext.Database.AppendSessionTokens(tokens);\n\n// after — guard with provider check\nif (context.Database.IsCosmos())\n{\n    context.Database.AppendSessionTokens(tokens);\n}","handlingStrategy":"validation","validationCode":"// Check provider before calling Cosmos-specific methods\nif (context.Database.IsCosmos())\n{\n    context.Database.AppendSessionTokens(tokens);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard all Cosmos-specific DatabaseFacade extension calls with database.IsCosmos().","Configure the DbContext with UseCosmos in OnConfiguring or AddDbContext before using Cosmos APIs.","Keep provider-specific code paths separated behind an abstraction to avoid cross-provider calls."],"tags":["ef-core","cosmos","provider-mismatch","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}