Skip to content

Commit

Permalink
Stop validating non-null/empty connection strings (#27874)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers authored Apr 25, 2022
1 parent 2a7c613 commit 54f5571
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 52 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions src/EFCore.Relational/Properties/RelationalStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -719,9 +719,6 @@
<data name="NoActiveTransaction" xml:space="preserve">
<value>The connection does not have any active transactions.</value>
</data>
<data name="NoConnectionOrConnectionString" xml:space="preserve">
<value>A relational store has been configured without specifying either the DbConnection or connection string to use.</value>
</data>
<data name="NoDbCommand" xml:space="preserve">
<value>Cannot create a DbCommand for a non-relational query.</value>
</data>
Expand Down Expand Up @@ -887,4 +884,4 @@
<data name="VisitChildrenMustBeOverridden" xml:space="preserve">
<value>'VisitChildren' must be overridden in the class deriving from 'SqlExpression'.</value>
</data>
</root>
</root>
21 changes: 2 additions & 19 deletions src/EFCore.Relational/Storage/RelationalConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,7 @@ public virtual string? ConnectionString
/// <returns>The connection string.</returns>
/// <exception cref="InvalidOperationException">when connection string cannot be obtained.</exception>
protected virtual string GetValidatedConnectionString()
{
var connectionString = ConnectionString;
if (connectionString == null)
{
throw new InvalidOperationException(RelationalStrings.NoConnectionOrConnectionString);
}

return connectionString;
}
=> ConnectionString!;

/// <summary>
/// Gets or sets the underlying <see cref="System.Data.Common.DbConnection" /> used to connect to the database.
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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();
Expand Down
13 changes: 0 additions & 13 deletions test/EFCore.Relational.Tests/RelationalConnectionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,19 +911,6 @@ public override void PopulateDebugInfo(IDictionary<string, string> 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<InvalidOperationException>(
() => connection.DbConnection).Message);

Assert.Null(connection.ConnectionString);
}

[ConditionalFact]
public void Puts_connection_string_on_connection_if_both_are_specified()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<InvalidOperationException>(
() => context.Customers.Any()).Message);
Assert.Throws<InvalidOperationException>(() => context.Customers.Any());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
3 changes: 2 additions & 1 deletion test/EFCore.Sqlite.FunctionalTests/TwoDatabasesSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 54f5571

Please sign in to comment.