Skip to content

MySqlConnectionStringBuilder.ConnectionString depends on order in which properties are set #1217

@bgrainger

Description

@bgrainger
var builder1 = new MySqlConnectionStringBuilder { Server = "localhost", UserID = "root" };
var builder2 = new MySqlConnectionStringBuilder { UserID = "root", Server = "localhost" };
Console.WriteLine(builder1.ConnectionString);
Console.WriteLine(builder2.ConnectionString);

produces:

Server=localhost;User ID=root
User ID=root;Server=localhost

It's unexpected that the serialized connection string would depend on the order in which C# properties are set.

This may be an artifact of some logic in the base DbConnectionStringBuilder class, possibly in an attempt to preserve connection strings set via the string constructor? (Although normalization of property names and punctuation does occur; see example below.) Or it could be an unintentional side-effect of using a Dictionary to store the options, which tends to return Keys in the order in which they were inserted, although this is certainly not guaranteed by the implementation.

var builder3 = new MySqlConnectionStringBuilder("Server=localhost;User ID=root");
var builder4 = new MySqlConnectionStringBuilder("User ID=root;Server=localhost");
var builder5 = new MySqlConnectionStringBuilder("uid = \"root\" ; Server = localhost");
Console.WriteLine(builder3.ConnectionString);
Console.WriteLine(builder4.ConnectionString);
Console.WriteLine(builder5.ConnectionString);

produces:

Server=localhost;User ID=root
User ID=root;Server=localhost
User ID=root;Server=localhost

Since option key names and punctuation are already normalized by the implementation, it makes the most sense to also output the options themselves in a consistent order; OrdinalIgnoreCase would be simplest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions