Visitar URL original
Override DbConnection.BeginTransactionAsync on SqlConnection · Issue #3248 · dotnet/SqlClient · GitHub
Skip to content

Override DbConnection.BeginTransactionAsync on SqlConnection #3248

Closed as duplicate
Closed as duplicate

Description

@RyanLamansky

Is your feature request related to a problem? Please describe.

This triggers CA1849 if the rule is enabled (included in the "All" ruleset) and called within an async method.

await using var transaction = connection.BeginTransaction();

The base DbConnection class provides BeginTransactionAsync, but since it doesn't have an implementation on SqlConnection, the return type is DbTransaction rather than SqlTransaction, which requires a cast to assign the transaction to a command later.

await using var transaction = (SqlTransaction)await connection.BeginTransactionAsync(cancellationToken);
await using var command = connection.CreateCommand();
command.Transaction = transaction;

Describe the solution you'd like

Cover the base BeginTransactionAsync with one that returns SqlTransaction when called directly on a SqlConnection instance.

await using var transaction = await connection.BeginTransactionAsync(cancellationToken);
await using var command = connection.CreateCommand();
command.Transaction = transaction;

Describe alternatives you've considered

It can be worked around with a cast, but that's writing code that really shouldn't be there.

Additional context

I'd make a PR for this myself but I'm somewhat intimidated working on such a large, unfamiliar project 😅

Activity

  1. added theissue type on Mar 27, 2025
  2. self-assigned this
    on Mar 27, 2025
  3. changed the title [-]Improve Compatiblity with Warning CA1849 on BeginTransaction[/-] [+]Override DbConnection.BeginTransactionAsync on SqlConnection[/+] on Mar 27, 2025
  4. mdaigle commented on Mar 27, 2025

    @mdaigle
    Contributor

    Thanks @RyanLamansky, I've added it to our backlog!

  5. roji commented on Mar 27, 2025

    @roji
    Member

    Duplicate of #113, no?

  6. mdaigle commented on Mar 27, 2025

    @mdaigle
    Contributor

    Thanks @roji, yes, I'll close this in favor of 113.

  7. RyanLamansky commented on Mar 28, 2025

    @RyanLamansky
    Author

    Ah, it's part of an old issue then. I had searched specifically for BeginTransactionAsync and nothing came back.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions