{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Extensions/RelationalDatabaseFacadeExtensions.cs#L857-L893","documentation":"Thrown by RelationalDatabaseFacadeExtensions.UseTransaction(DbTransaction, Guid) when the context's transaction manager is not an IRelationalTransactionManager — meaning the context is not using a relational database provider (e.g. it is using the InMemory provider). Relational transaction APIs only work when a relational provider is registered.","triggerScenarios":"Calling Database.UseTransaction(dbTransaction, transactionId) on a context whose provider is non-relational (Microsoft.EntityFrameworkCore.InMemory, or a non-relational custom provider).","commonSituations":"Unit tests using UseInMemoryDatabase that also call UseTransaction; switching a context from SQL Server/SQLite to InMemory without removing transaction-management code; a provider that does not implement IRelationalTransactionManager.","solutions":["Use a relational provider (UseSqlServer, UseSqlite, UseNpgsql, etc.) so the transaction manager is relational.","Guard relational transaction calls behind a check of whether the provider is relational.","Remove UseTransaction calls in test scenarios that run against the InMemory provider."],"exampleFix":"// before: InMemory provider cannot accept a DbTransaction\noptionsBuilder.UseInMemoryDatabase(\"test\");\ncontext.Database.UseTransaction(existingDbTransaction, id); // throws\n\n// after: use a relational provider in the scenario needing transactions\noptionsBuilder.UseSqlite(\"DataSource=:memory:\");\ncontext.Database.UseTransaction(existingDbTransaction, id);","handlingStrategy":"validation","validationCode":"// Only enlist a DbTransaction when the provider is relational\nvar provider = context.Database.GetService<IRelationalConnection>();\nif (provider != null)\n    context.Database.UseTransaction(dbTransaction, transactionId);\nelse\n    throw new InvalidOperationException(\"Context is not using a relational provider.\");","typeGuard":"static bool IsRelational(DbContext c)\n    => c.Database.GetService<IRelationalDatabaseFacadeDependencies>() != null;","tryCatchPattern":null,"preventionTips":["Use a relational provider wherever transaction APIs are needed.","Branch transaction enlistment behind an IsRelational check in shared code.","Keep UseTransaction calls out of InMemory-based test paths."],"tags":["ef-core","relational","transactions","provider","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}