diff --git a/doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml b/doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml index 6db9533606..35b8d24ab6 100644 --- a/doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml +++ b/doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml @@ -7,7 +7,7 @@ property. When converting from a boolean, a value of `true` converts to and a value of `false` converts to . When converting to a boolean, and convert to `true` and converts `false`. +Implicit conversions have been added to maintain backwards compatibility with boolean behahavior for the property. When converting from a boolean, a value of `true` converts to and a value of `false` converts to . When converting to a boolean, , , and `null` convert to `true` and converts `false`. ]]> diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs index 28a8fbc6fe..f89224cd97 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlConnectionStringBuilder.cs @@ -1237,8 +1237,9 @@ public SqlConnectionEncryptOption Encrypt get => _encrypt; set { - SetSqlConnectionEncryptionValue(value); - _encrypt = value; + SqlConnectionEncryptOption newValue = value ?? DbConnectionStringDefaults.Encrypt; + SetSqlConnectionEncryptionValue(newValue); + _encrypt = newValue; } } diff --git a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs index 828152c4ab..1608525404 100644 --- a/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/FunctionalTests/SqlConnectionStringBuilderTest.cs @@ -383,6 +383,10 @@ public void ConnectionBuilderEncryptBackwardsCompatibility() builder.Encrypt = SqlConnectionEncryptOption.Strict; Assert.Equal("Encrypt=Strict", builder.ConnectionString); Assert.True(builder.Encrypt); + + builder.Encrypt = null; + Assert.Equal("Encrypt=True", builder.ConnectionString); + Assert.True(builder.Encrypt); } [Theory] @@ -414,7 +418,7 @@ public void EncryptParserInvalidValuesThrowsException(string value) [InlineData("strict", "Strict")] public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedValue) { - Assert.True(SqlConnectionEncryptOption.TryParse(value, out var result)); + Assert.True(SqlConnectionEncryptOption.TryParse(value, out SqlConnectionEncryptOption result)); Assert.Equal(expectedValue, result.ToString()); } @@ -424,7 +428,10 @@ public void EncryptTryParseValidValuesReturnsTrue(string value, string expectedV [InlineData(null)] [InlineData(" true ")] public void EncryptTryParseInvalidValuesReturnsFalse(string value) - => Assert.False(SqlConnectionEncryptOption.TryParse(value, out _)); + { + Assert.False(SqlConnectionEncryptOption.TryParse(value, out SqlConnectionEncryptOption result)); + Assert.Null(result); + } internal void ExecuteConnectionStringTests(string connectionString) {