Repository: dotnet/efcore
File: src/EFCore.SqlServer/Storage/Internal/SqlServerConnection.cs (C#)

Code before:
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </remarks>
    [EntityFrameworkInternal]
    protected virtual string ModifyConnectionString(string userProvidedConnectionString)
    {
        var connectionStringBuilder = new SqlConnectionStringBuilder(userProvidedConnectionString);

        // SqlClient sends "Core Microsoft SqlClient Data Provider" as the default Application Name when unset;
        // detect that and overwrite.
        if (connectionStringBuilder.ApplicationName is "Core Microsoft SqlClient Data Provider" or "" or null)
        {
            var efVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;
            var sqlClientVersion = FileVersionInfo.GetVersionInfo(typeof(SqlConnection).Assembly.Location).ProductVersion;

            connectionStringBuilder.ApplicationName =
                $"EFCore/{efVersion} ({RuntimeInformation.OSDescription} {RuntimeInformation.OSArchitecture}) {RuntimeInformation.FrameworkDescription} SqlClient/{sqlClientVersion}";

            return connectionStringBuilder.ToString();
        }

        return userProvidedConnectionString;
    }

    /// <summary>
    ///     If the user provided a connection string (either directly or via a SqlConnection that has one),
    ///     re-set it via the ConnectionString property, which we override above to add the Application Name
    ///     with EF version information.
    /// </summary>
    private void RecalculateConnectionString()
    {
        if (ConnectionString is string userProvidedConnectionString)
        {
            ConnectionString = userProvidedConnectionString;
        }
    }
}


Review comment (performance):
The application name string is constructed using reflection and system information on every connection string modification. Consider caching this computed application name as a static field since the version information won't change during application lifetime.
```suggestion
            connectionStringBuilder.ApplicationName = _cachedApplicationName;
```

Code after:
    protected override bool SupportsAmbientTransactions
        => true;

    /// <summary>
    ///     Modifies the user-provided connection string. By default, adds an Application Name to the connection
    ///     string containing EF Core and versioning information.
    /// </summary>
    /// <remarks>
    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
    ///     any release. You should only use it directly in your code with extreme caution and knowing that
    ///     doing so can result in application failures when updating to a new Entity Framework Core release.
    /// </remarks>
    [EntityFrameworkInternal]
    protected virtual string ModifyConnectionString(string userProvidedConnectionString)
    {
        try
        {
            var connectionStringBuilder = new SqlConnectionStringBuilder(userProvidedConnectionString);

            // SqlClient sends "Core Microsoft SqlClient Data Provider" as the default Application Name when unset;
            // detect that and overwrite.
            if (connectionStringBuilder.ApplicationName is "Core Microsoft SqlClient Data Provider" or "" or null)
            {
                var efVersion = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location).ProductVersion;

                _defaultApplicationName ??=
                    $"EFCore/{efVersion} ({RuntimeInformation.OSDescription} {RuntimeInformation.OSArchitecture})";

                connectionStringBuilder.ApplicationName = _defaultApplicationName;

                return connectionStringBuilder.ToString();
            }
        }
        catch
        {
            // If anything goes wrong, simply don't modify the connection string.
            // There are some scenarios where an invalid string is provided and an exception isn't expected at this phase
            // (see e.g. test GetContextInfo_does_not_throw_if_DbConnection_cannot_be_created)
        }

        return userProvidedConnectionString;
    }
}