Skip to content

Inject Application Name to SQL Server connection string when not set - #36548

Merged
roji merged 1 commit into
dotnet:mainfrom
roji:SqlClientConnectionString
Aug 14, 2025
Merged

roji merged 1 commit into
dotnet:mainfrom
roji:SqlClientConnectionString

Conversation

@roji

@roji roji commented Aug 11, 2025

Copy link
Copy Markdown
Member

Note: this support via the connection string is temporary and is likely to go away in EF 11 once SqlClient introduce a more first-class user-agent-like mechanism (see #35730 (comment)).

Closes #35730

@roji
roji requested review from a team and Copilot August 11, 2025 07:00

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements automatic injection of application name information into SQL Server connection strings when not explicitly set by the user. The application name includes EF Core version, OS details, framework information, and SqlClient version to aid in monitoring and diagnostics.

  • Adds automatic Application Name injection to SQL Server connections containing EF Core and environment details
  • Modifies connection string handling to preserve user-defined Application Names while adding EF defaults when missing
  • Includes comprehensive test coverage for both connection string and DbConnection scenarios

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
src/EFCore.SqlServer/Storage/Internal/SqlServerConnection.cs Core implementation adding connection string modification logic and Application Name injection
test/EFCore.SqlServer.Tests/Storage/Internal/SqlServerConnectionTest.cs Test coverage for Application Name injection scenarios and helper method for existing tests

@roji

roji commented Aug 11, 2025

Copy link
Copy Markdown
Member Author

Hmm, test failures indicate that unfortunate there's a 128-char limit on Application Name (The value's length for key 'Application Name' exceeds its limit of '128').

So instead of a nice long EFCore/10.0.0-dev (macOS 15.6.0 Arm64) .NET 10.0.0-preview.6.25358.103 SqlClient/6.1.0+1af7327454f9cafbf3aaaa8b3d615489aad480ab, we'll just do EFCore/10.0.0-dev (macOS 15.6.0 Arm64) for now.

@ErikEJ

ErikEJ commented Aug 11, 2025

Copy link
Copy Markdown
Contributor

@roji Could you note use a shorther representation of the SqlClient version?

@roji
roji force-pushed the SqlClientConnectionString branch 2 times, most recently from a36ce56 to daf752b Compare August 13, 2025 09:05
@roji

roji commented Aug 13, 2025

Copy link
Copy Markdown
Member Author

@AndriySvyryd @cincuranet @ErikEJ pushed changes to only rewrite when the user configures EF with a connection string, as discussed, should be ready for review.

// Application Name with EF and versioning info.
// Note that we don't do anything if EF was configured with a SqlConnection, as that could reset the
// password because of Persist Security Info=true
var relationalOptions = RelationalOptionsExtension.Extract(dependencies.ContextOptions);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love that we need to extract the options here again (or the smelly assignment from ConnectionString to ConnectionString), but RelationalConnection works hard to prevent implementations from ever knowing if it was configured with a connection string or connection and generally to implement this kind of feature.

I'd propose some refactorings here, but this is temporary throwaway code, since SqlClient will be including a first-class user-agent-like feature anyway - so as a stop-gap this seems ok...

@ErikEJ

ErikEJ commented Aug 13, 2025

Copy link
Copy Markdown
Contributor

LOTM

@roji
roji force-pushed the SqlClientConnectionString branch from daf752b to f41e45e Compare August 13, 2025 09:56
@roji
roji force-pushed the SqlClientConnectionString branch from f41e45e to 38632ed Compare August 13, 2025 13:54
@roji

roji commented Aug 13, 2025

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@roji
roji enabled auto-merge (squash) August 13, 2025 19:22

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good enough for now

cheenamalhotra added a commit that referenced this pull request Nov 17, 2026
- Preserve connection strings without injecting EF Core version and platform details.
- Restore interceptor coverage and retain registered-application telemetry from the parent layer.

Reverts #36548

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
AndriySvyryd pushed a commit that referenced this pull request Nov 17, 2026
- Preserve connection strings without injecting EF Core version and platform details.
- Restore interceptor coverage and retain registered-application telemetry from the parent layer.

Reverts #36548

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SQL Server: Introduce EF and its version into the connection string's Application Name

5 participants