Skip to content

Commit

Permalink
Allow null connection string to be passed to UseProvider methods
Browse files Browse the repository at this point in the history
Fixes #26869
  • Loading branch information
ajcvickers committed Jul 15, 2022
1 parent 442a2f7 commit bf5caf5
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ public static DbContextOptionsBuilder UseSqlServer(
/// <returns>The options builder so that further configuration can be chained.</returns>
public static DbContextOptionsBuilder UseSqlServer(
this DbContextOptionsBuilder optionsBuilder,
string connectionString,
string? connectionString,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
{
Check.NotEmpty(connectionString, nameof(connectionString));

var extension = (SqlServerOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnectionString(connectionString);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

Expand Down Expand Up @@ -152,7 +150,7 @@ public static DbContextOptionsBuilder<TContext> UseSqlServer<TContext>(
/// <returns>The options builder so that further configuration can be chained.</returns>
public static DbContextOptionsBuilder<TContext> UseSqlServer<TContext>(
this DbContextOptionsBuilder<TContext> optionsBuilder,
string connectionString,
string? connectionString,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseSqlServer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,16 @@ public static class SqlServerServiceCollectionExtensions
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddSqlServer<TContext>(
this IServiceCollection serviceCollection,
string connectionString,
string? connectionString,
Action<SqlServerDbContextOptionsBuilder>? sqlServerOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
{
Check.NotEmpty(connectionString, nameof(connectionString));

return serviceCollection.AddDbContext<TContext>(
=> serviceCollection.AddDbContext<TContext>(
(_, options) =>
{
optionsAction?.Invoke(options);
options.UseSqlServer(connectionString, sqlServerOptionsAction);
});
}

/// <summary>
/// <para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@ public static DbContextOptionsBuilder UseSqlite(
/// <returns>The options builder so that further configuration can be chained.</returns>
public static DbContextOptionsBuilder UseSqlite(
this DbContextOptionsBuilder optionsBuilder,
string connectionString,
string? connectionString,
Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
{
Check.NotEmpty(connectionString, nameof(connectionString));

var extension = (SqliteOptionsExtension)GetOrCreateExtension(optionsBuilder).WithConnectionString(connectionString);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);

Expand Down Expand Up @@ -145,7 +143,7 @@ public static DbContextOptionsBuilder<TContext> UseSqlite<TContext>(
/// <returns>The options builder so that further configuration can be chained.</returns>
public static DbContextOptionsBuilder<TContext> UseSqlite<TContext>(
this DbContextOptionsBuilder<TContext> optionsBuilder,
string connectionString,
string? connectionString,
Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseSqlite(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,16 @@ public static class SqliteServiceCollectionExtensions
/// <returns>The same service collection so that multiple calls can be chained.</returns>
public static IServiceCollection AddSqlite<TContext>(
this IServiceCollection serviceCollection,
string connectionString,
string? connectionString,
Action<SqliteDbContextOptionsBuilder>? sqliteOptionsAction = null,
Action<DbContextOptionsBuilder>? optionsAction = null)
where TContext : DbContext
{
Check.NotEmpty(connectionString, nameof(connectionString));

return serviceCollection.AddDbContext<TContext>(
=> serviceCollection.AddDbContext<TContext>(
(_, options) =>
{
optionsAction?.Invoke(options);
options.UseSqlite(connectionString, sqliteOptionsAction);
});
}

/// <summary>
/// <para>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public virtual void Can_query_from_one_connection_and_save_changes_to_another()
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public virtual void Can_set_connection_string_in_interceptor(bool withConnectionString)
[InlineData(true, false)]
[InlineData(true, false)]
[InlineData(true, true)]
public virtual void Can_set_connection_string_in_interceptor(bool withConnectionString, bool withNullConnectionString)
{
using var context1 = CreateBackingContext("TwoDatabasesIntercept");

Expand Down Expand Up @@ -132,7 +133,8 @@ public override void ConnectionClosed(DbConnection connection, ConnectionEndEven

protected abstract DbContextOptionsBuilder CreateTestOptions(
DbContextOptionsBuilder optionsBuilder,
bool withConnectionString = false);
bool withConnectionString = false,
bool withNullConnectionString = false);

protected abstract TwoDatabasesWithDataContext CreateBackingContext(string databaseName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public TwoDatabasesSqlServerTest(SqlServerFixture fixture)

protected override DbContextOptionsBuilder CreateTestOptions(
DbContextOptionsBuilder optionsBuilder,
bool withConnectionString = false)
bool withConnectionString = false,
bool withNullConnectionString = false)
=> withConnectionString
? optionsBuilder.UseSqlServer(DummyConnectionString)
? withNullConnectionString
? optionsBuilder.UseSqlServer((string)null)
: optionsBuilder.UseSqlServer(DummyConnectionString)
: optionsBuilder.UseSqlServer();

protected override TwoDatabasesWithDataContext CreateBackingContext(string databaseName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,17 @@ public void Can_add_extension_with_connection_string()
Assert.Null(extension.Connection);
}

[ConditionalFact]
public void Can_add_extension_with_connection_string_using_generic_options()
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public void Can_add_extension_with_connection_string_using_generic_options(bool nullConnectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
optionsBuilder.UseSqlServer("Database=Whisper");
optionsBuilder.UseSqlServer(nullConnectionString ? null : "Database=Whisper");

var extension = optionsBuilder.Options.Extensions.OfType<SqlServerOptionsExtension>().Single();

Assert.Equal("Database=Whisper", extension.ConnectionString);
Assert.Equal(nullConnectionString ? null : "Database=Whisper", extension.ConnectionString);
Assert.Null(extension.Connection);
}

Expand Down Expand Up @@ -83,12 +85,14 @@ public void Can_add_extension_with_connection_using_generic_options()
Assert.Null(extension.ConnectionString);
}

[ConditionalFact]
public void Service_collection_extension_method_can_configure_sqlserver_options()
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public void Service_collection_extension_method_can_configure_sqlserver_options(bool nullConnectionString)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSqlServer<ApplicationDbContext>(
"Database=Crunchie",
nullConnectionString ? null : "Database=Crunchie",
sqlServerOption =>
{
sqlServerOption.MaxBatchSize(123);
Expand All @@ -115,7 +119,7 @@ public void Service_collection_extension_method_can_configure_sqlserver_options(

Assert.Equal(123, sqlServerOptions.MaxBatchSize);
Assert.Equal(30, sqlServerOptions.CommandTimeout);
Assert.Equal("Database=Crunchie", sqlServerOptions.ConnectionString);
Assert.Equal(nullConnectionString ? null : "Database=Crunchie", sqlServerOptions.ConnectionString);
}
}

Expand Down
7 changes: 5 additions & 2 deletions test/EFCore.Sqlite.FunctionalTests/TwoDatabasesSqliteTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ public TwoDatabasesSqliteTest(TwoDatabasesFixture fixture)

protected override DbContextOptionsBuilder CreateTestOptions(
DbContextOptionsBuilder optionsBuilder,
bool withConnectionString = false)
bool withConnectionString = false,
bool withNullConnectionString = false)
=> withConnectionString
? optionsBuilder.UseSqlite(DummyConnectionString)
? withNullConnectionString
? optionsBuilder.UseSqlite((string)null)
: optionsBuilder.UseSqlite(DummyConnectionString)
: optionsBuilder.UseSqlite();

protected override TwoDatabasesWithDataContext CreateBackingContext(string databaseName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,17 @@ public void Can_add_extension_with_connection_string()
Assert.Null(extension.Connection);
}

[ConditionalFact]
public void Can_add_extension_with_connection_string_using_generic_options()
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public void Can_add_extension_with_connection_string_using_generic_options(bool nullConnectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
optionsBuilder.UseSqlite("Database=Whisper");
optionsBuilder.UseSqlite(nullConnectionString ? null : "Database=Whisper");

var extension = optionsBuilder.Options.Extensions.OfType<SqliteOptionsExtension>().Single();

Assert.Equal("Database=Whisper", extension.ConnectionString);
Assert.Equal(nullConnectionString ? null : "Database=Whisper", extension.ConnectionString);
Assert.Null(extension.Connection);
}

Expand Down Expand Up @@ -95,12 +97,14 @@ public void Can_add_extension_with_connection_using_generic_options()
Assert.Null(extension.ConnectionString);
}

[ConditionalFact]
public void Service_collection_extension_method_can_configure_sqlite_options()
[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public void Service_collection_extension_method_can_configure_sqlite_options(bool nullConnectionString)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddSqlite<ApplicationDbContext>(
"Database=Crunchie",
nullConnectionString ? null : "Database=Crunchie",
sqliteOptions =>
{
sqliteOptions.MaxBatchSize(123);
Expand All @@ -127,7 +131,7 @@ public void Service_collection_extension_method_can_configure_sqlite_options()

Assert.Equal(123, sqliteOptions.MaxBatchSize);
Assert.Equal(30, sqliteOptions.CommandTimeout);
Assert.Equal("Database=Crunchie", sqliteOptions.ConnectionString);
Assert.Equal(nullConnectionString ? null : "Database=Crunchie", sqliteOptions.ConnectionString);
}
}

Expand Down

0 comments on commit bf5caf5

Please sign in to comment.