Skip to content

Conversation

roji
Copy link
Member

@roji roji commented Aug 11, 2025

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
Copy link

@Copilot Copilot AI left a comment

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
Copy link
Member Author

roji commented Aug 11, 2025

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
Copy link
Contributor

ErikEJ commented Aug 11, 2025

@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
Copy link
Member Author

roji commented Aug 13, 2025

@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
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
Copy link
Contributor

ErikEJ commented Aug 13, 2025

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
Copy link
Member Author

roji commented Aug 13, 2025

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

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

@AndriySvyryd AndriySvyryd left a comment

Choose a reason for hiding this comment

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

Good enough for now

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

3 participants