{"record":{"id":"e26980ac87d59e50","repo":"dotnet/efcore","slug":"relational-specific-methods-can-only-be-used-when","errorCode":null,"errorMessage":"Relational-specific methods can only be used when the context is using a relational database provider.","messagePattern":"Relational-specific methods can only be used when the context is using a relational database provider\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs","lineNumber":875,"sourceCode":"        => databaseFacade.UseTransaction(transaction, Guid.NewGuid());\n\n    /// <summary>\n    ///     Sets the <see cref=\"DbTransaction\" /> to be used by database operations on the <see cref=\"DbContext\" />.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-transactions\">Transactions in EF Core</see> for more information and examples.\n    /// </remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>\n    /// <param name=\"transaction\">The <see cref=\"DbTransaction\" /> to use.</param>\n    /// <param name=\"transactionId\">The unique identifier for the transaction.</param>\n    /// <returns>A <see cref=\"IDbContextTransaction\" /> that encapsulates the given transaction.</returns>\n    public static IDbContextTransaction? UseTransaction(\n        this DatabaseFacade databaseFacade,\n        DbTransaction? transaction,\n        Guid transactionId)\n        => databaseFacade.GetTransactionManager() is IRelationalTransactionManager relationalTransactionManager\n            ? relationalTransactionManager.UseTransaction(transaction, transactionId)\n            : throw new InvalidOperationException(RelationalStrings.RelationalNotInUse);\n\n    /// <summary>\n    ///     Sets the <see cref=\"DbTransaction\" /> to be used by database operations on the <see cref=\"DbContext\" />.\n    /// </summary>\n    /// <remarks>\n    ///     See <see href=\"https://aka.ms/efcore-docs-transactions\">Transactions in EF Core</see> for more information and examples.\n    /// </remarks>\n    /// <param name=\"databaseFacade\">The <see cref=\"DatabaseFacade\" /> for the context.</param>\n    /// <param name=\"transaction\">The <see cref=\"DbTransaction\" /> to use.</param>\n    /// <param name=\"cancellationToken\">A <see cref=\"CancellationToken\" /> to observe while waiting for the task to complete.</param>\n    /// <returns>A <see cref=\"Task\" /> containing the <see cref=\"IDbContextTransaction\" /> for the given transaction.</returns>\n    /// <exception cref=\"OperationCanceledException\">If the <see cref=\"CancellationToken\" /> is canceled.</exception>\n    public static Task<IDbContextTransaction?> UseTransactionAsync(\n        this DatabaseFacade databaseFacade,\n        DbTransaction? transaction,\n        CancellationToken cancellationToken = default)\n        => databaseFacade.UseTransactionAsync(transaction, Guid.NewGuid(), cancellationToken);\n","sourceCodeStart":857,"sourceCodeEnd":893,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs#L857-L893","documentation":"Thrown by DatabaseFacade.UseTransaction(DbTransaction, Guid) when the context's transaction manager is not an IRelationalTransactionManager, i.e. the context is using a non-relational provider (e.g. in-memory or Cosmos). Relational transaction APIs only work with relational providers.","triggerScenarios":"Calling UseTransaction with an ambient DbTransaction on a context backed by a non-relational provider (RelationalDatabaseFacadeExtensions.cs:873-875). The cast to IRelationalTransactionManager fails, so RelationalNotInUse is thrown.","commonSituations":"Using the in-memory provider in tests while code paths call UseTransaction; switching provider config at runtime; Cosmos DB provider used with relational transaction sharing code; environment-specific provider selection (Test vs Prod) where tests use in-memory.","solutions":["Use a relational provider (SqlServer, Sqlite, etc.) when transaction sharing is required.","Guard the call with databaseFacade.IsRelational() before invoking UseTransaction.","In tests, replace transaction-sharing logic with a relational test database or skip it for in-memory.","Ensure the correct UseSqlServer/UseSqlite call is active in OnConfiguring for the environment."],"exampleFix":"// before\ndb.Database.UseTransaction(existingDbTransaction); // throws in tests\n\n// after\nif (db.Database.IsRelational())\n    await db.Database.UseTransactionAsync(existingDbTransaction);","handlingStrategy":"type-guard","validationCode":"if (db.Database.IsRelational())\n    db.Database.UseTransaction(transaction, transactionId);","typeGuard":"static bool CanShareTransaction(DatabaseFacade f) => f.IsRelational();","tryCatchPattern":null,"preventionTips":["Always guard relational APIs with IsRelational().","Use a relational provider in integration tests that exercise transaction sharing.","Centralize transaction handling in a provider-aware helper."],"tags":["efcore","relational","transactions","provider","in-memory"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}