{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Extensions/CosmosDatabaseFacadeExtensions.cs#L78-L114","documentation":"Thrown by GetSessionTokenStorage when the configured IDatabase is not a CosmosDatabaseWrapper. All Cosmos session-token APIs (GetSessionToken, UseSessionToken, AppendSessionToken, etc.) require the Cosmos provider; calling them on a context backed by another provider is a misuse. Fires the moment the extension resolves the Cosmos-specific storage.","triggerScenarios":"Calling database.GetSessionToken(), database.UseSessionToken(...), database.AppendSessionToken(...), etc. on a DbContext that was configured with a non-Cosmos provider (SQL Server, SQLite, InMemory, etc.).","commonSituations":"Sharing an extension method across provider-specific contexts. Switching a context from Cosmos to another provider during refactoring without removing Cosmos-specific calls. Test harnesses using InMemory provider but exercising Cosmos session-token code paths.","solutions":["Only call Cosmos session-token APIs on a context configured with UseCosmos().","Guard calls with database.IsCosmos() before invoking session-token methods.","Move Cosmos-specific operations behind an interface/provider-strategy so non-Cosmos contexts never reach this code."],"exampleFix":"// before\nvar token = context.Database.GetSessionToken(); // throws if not Cosmos\n\n// after\nif (context.Database.IsCosmos())\n{\n    var token = context.Database.GetSessionToken();\n}","handlingStrategy":"type-guard","validationCode":"if (!context.Database.IsCosmos())\n{\n    throw new InvalidOperationException(\"Session-token APIs require the Cosmos provider.\");\n}\nvar token = context.Database.GetSessionToken();","typeGuard":"static bool IsCosmosContext(DbContext context)\n    => context.Database.ProviderName == \"Microsoft.EntityFrameworkCore.Cosmos\";","tryCatchPattern":null,"preventionTips":["Guard every Cosmos-specific DatabaseFacade call with database.IsCosmos().","Keep Cosmos session-token logic inside a provider-specific code path.","Add a test that runs session-token code only against a Cosmos-configured context."],"tags":["ef-core","cosmos","provider","session-token","configuration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}