From 54f5571dadb57fcae392774f9812f0fc4187f77a Mon Sep 17 00:00:00 2001 From: Arthur Vickers Date: Mon, 25 Apr 2022 18:59:34 +0100 Subject: [PATCH] Stop validating non-null/empty connection strings (#27874) --- .../Properties/RelationalStrings.Designer.cs | 6 ------ .../Properties/RelationalStrings.resx | 5 +---- .../Storage/RelationalConnection.cs | 21 ++----------------- .../TwoDatabasesTestBase.cs | 10 +++++---- .../RelationalConnectionTest.cs | 13 ------------ .../ConnectionSpecificationTest.cs | 5 +---- .../TwoDatabasesSqlServerTest.cs | 3 ++- .../TwoDatabasesSqliteTest.cs | 3 ++- 8 files changed, 14 insertions(+), 52 deletions(-) diff --git a/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs b/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs index 0ca37357f9e..a6e796bbf37 100644 --- a/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs +++ b/src/EFCore.Relational/Properties/RelationalStrings.Designer.cs @@ -945,12 +945,6 @@ public static string NestedAmbientTransactionError public static string NoActiveTransaction => GetString("NoActiveTransaction"); - /// - /// A relational store has been configured without specifying either the DbConnection or connection string to use. - /// - public static string NoConnectionOrConnectionString - => GetString("NoConnectionOrConnectionString"); - /// /// Cannot create a DbCommand for a non-relational query. /// diff --git a/src/EFCore.Relational/Properties/RelationalStrings.resx b/src/EFCore.Relational/Properties/RelationalStrings.resx index f593fd008f0..e694c4b7af9 100644 --- a/src/EFCore.Relational/Properties/RelationalStrings.resx +++ b/src/EFCore.Relational/Properties/RelationalStrings.resx @@ -719,9 +719,6 @@ The connection does not have any active transactions. - - A relational store has been configured without specifying either the DbConnection or connection string to use. - Cannot create a DbCommand for a non-relational query. @@ -887,4 +884,4 @@ 'VisitChildren' must be overridden in the class deriving from 'SqlExpression'. - \ No newline at end of file + diff --git a/src/EFCore.Relational/Storage/RelationalConnection.cs b/src/EFCore.Relational/Storage/RelationalConnection.cs index 3774c3a5c7f..934c4654716 100644 --- a/src/EFCore.Relational/Storage/RelationalConnection.cs +++ b/src/EFCore.Relational/Storage/RelationalConnection.cs @@ -125,15 +125,7 @@ public virtual string? ConnectionString /// The connection string. /// when connection string cannot be obtained. protected virtual string GetValidatedConnectionString() - { - var connectionString = ConnectionString; - if (connectionString == null) - { - throw new InvalidOperationException(RelationalStrings.NoConnectionOrConnectionString); - } - - return connectionString; - } + => ConnectionString!; /// /// Gets or sets the underlying used to connect to the database. @@ -149,16 +141,7 @@ protected virtual string GetValidatedConnectionString() [AllowNull] public virtual DbConnection DbConnection { - get - { - if (_connection == null - && _connectionString == null) - { - throw new InvalidOperationException(RelationalStrings.NoConnectionOrConnectionString); - } - - return _connection ??= CreateDbConnection(); - } + get => _connection ??= CreateDbConnection(); set { if (!ReferenceEquals(_connection, value)) diff --git a/test/EFCore.Relational.Specification.Tests/TwoDatabasesTestBase.cs b/test/EFCore.Relational.Specification.Tests/TwoDatabasesTestBase.cs index 6cd03c2972f..7f73a84a68b 100644 --- a/test/EFCore.Relational.Specification.Tests/TwoDatabasesTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/TwoDatabasesTestBase.cs @@ -73,8 +73,10 @@ public virtual void Can_query_from_one_connection_and_save_changes_to_another() Assert.Equal(new[] { "Modified One", "Modified Two" }, context2.Foos.Select(e => e.Bar).ToList()); } - [ConditionalFact] - public virtual void Can_set_connection_string_in_interceptor() + [ConditionalTheory] + [InlineData(false)] + [InlineData(true)] + public virtual void Can_set_connection_string_in_interceptor(bool withConnectionString) { using var context1 = CreateBackingContext("TwoDatabasesIntercept"); @@ -83,10 +85,10 @@ public virtual void Can_set_connection_string_in_interceptor() context1.Database.EnsureCreatedResiliently(); using (var context = new TwoDatabasesContext( - CreateTestOptions(new DbContextOptionsBuilder(), withConnectionString: true) + CreateTestOptions(new DbContextOptionsBuilder(), withConnectionString) .AddInterceptors( new ConnectionStringConnectionInterceptor( - connectionString1, DummyConnectionString)) + connectionString1, withConnectionString ? DummyConnectionString : "")) .Options)) { var data = context.Foos.ToList(); diff --git a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs index b7ece09bc60..36003046036 100644 --- a/test/EFCore.Relational.Tests/RelationalConnectionTest.cs +++ b/test/EFCore.Relational.Tests/RelationalConnectionTest.cs @@ -911,19 +911,6 @@ public override void PopulateDebugInfo(IDictionary debugInfo) } } - [ConditionalFact] - public void Throws_if_no_connection_or_connection_string_is_specified_only_when_accessed() - { - var connection = new FakeRelationalConnection(CreateOptions(new FakeRelationalOptionsExtension())); - - Assert.Equal( - RelationalStrings.NoConnectionOrConnectionString, - Assert.Throws( - () => connection.DbConnection).Message); - - Assert.Null(connection.ConnectionString); - } - [ConditionalFact] public void Puts_connection_string_on_connection_if_both_are_specified() { diff --git a/test/EFCore.SqlServer.FunctionalTests/ConnectionSpecificationTest.cs b/test/EFCore.SqlServer.FunctionalTests/ConnectionSpecificationTest.cs index 2916091d6ac..044879b4323 100644 --- a/test/EFCore.SqlServer.FunctionalTests/ConnectionSpecificationTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/ConnectionSpecificationTest.cs @@ -51,10 +51,7 @@ public void Throws_if_context_used_with_no_connection_or_connection_string() { using var context = new NoneInOnConfiguringContext(); - Assert.Equal( - RelationalStrings.NoConnectionOrConnectionString, - Assert.Throws( - () => context.Customers.Any()).Message); + Assert.Throws(() => context.Customers.Any()); } } diff --git a/test/EFCore.SqlServer.FunctionalTests/TwoDatabasesSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/TwoDatabasesSqlServerTest.cs index 29a2548bc8d..6eee3a0296e 100644 --- a/test/EFCore.SqlServer.FunctionalTests/TwoDatabasesSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/TwoDatabasesSqlServerTest.cs @@ -23,5 +23,6 @@ protected override DbContextOptionsBuilder CreateTestOptions( protected override TwoDatabasesWithDataContext CreateBackingContext(string databaseName) => new(Fixture.CreateOptions(SqlServerTestStore.Create(databaseName))); - protected override string DummyConnectionString { get; } = "Database=DoesNotExist"; + protected override string DummyConnectionString + => "Database=DoesNotExist"; } diff --git a/test/EFCore.Sqlite.FunctionalTests/TwoDatabasesSqliteTest.cs b/test/EFCore.Sqlite.FunctionalTests/TwoDatabasesSqliteTest.cs index 5408a14299d..42eeb94b195 100644 --- a/test/EFCore.Sqlite.FunctionalTests/TwoDatabasesSqliteTest.cs +++ b/test/EFCore.Sqlite.FunctionalTests/TwoDatabasesSqliteTest.cs @@ -23,7 +23,8 @@ protected override DbContextOptionsBuilder CreateTestOptions( protected override TwoDatabasesWithDataContext CreateBackingContext(string databaseName) => new(Fixture.CreateOptions(SqliteTestStore.Create(databaseName))); - protected override string DummyConnectionString { get; } = "DataSource=DummyDatabase"; + protected override string DummyConnectionString + => "DataSource=DummyDatabase"; public class TwoDatabasesFixture : ServiceProviderFixtureBase {