Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression bug to allow usage of named connection strings again #1913

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/EFCore.MySql/Internal/MySqlOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using Microsoft.Extensions.DependencyInjection;
using MySqlConnector;

Expand Down Expand Up @@ -57,7 +58,7 @@ public virtual void Initialize(IDbContextOptions options)
var mySqlOptions = options.FindExtension<MySqlOptionsExtension>() ?? new MySqlOptionsExtension();
var mySqlJsonOptions = (MySqlJsonOptionsExtension)options.Extensions.LastOrDefault(e => e is MySqlJsonOptionsExtension);

ConnectionSettings = GetConnectionSettings(mySqlOptions);
ConnectionSettings = GetConnectionSettings(mySqlOptions, options);
DataSource = mySqlOptions.DataSource;
ServerVersion = mySqlOptions.ServerVersion ?? throw new InvalidOperationException($"The {nameof(ServerVersion)} has not been set.");
NoBackslashEscapes = mySqlOptions.NoBackslashEscapes;
Expand All @@ -77,7 +78,7 @@ public virtual void Validate(IDbContextOptions options)
{
var mySqlOptions = options.FindExtension<MySqlOptionsExtension>() ?? new MySqlOptionsExtension();
var mySqlJsonOptions = (MySqlJsonOptionsExtension)options.Extensions.LastOrDefault(e => e is MySqlJsonOptionsExtension);
var connectionSettings = GetConnectionSettings(mySqlOptions);
var connectionSettings = GetConnectionSettings(mySqlOptions, options);

//
// CHECK: To we have to ensure that the ApplicationServiceProvider itself is not replaced, because we rely on it in our
Expand Down Expand Up @@ -247,10 +248,12 @@ protected virtual MySqlDefaultDataTypeMappings ApplyDefaultDataTypeMappings(MySq
return defaultDataTypeMappings;
}

private static MySqlConnectionSettings GetConnectionSettings(MySqlOptionsExtension relationalOptions)
private static MySqlConnectionSettings GetConnectionSettings(MySqlOptionsExtension relationalOptions, IDbContextOptions options)
=> relationalOptions.Connection != null
? new MySqlConnectionSettings(relationalOptions.Connection)
: new MySqlConnectionSettings(relationalOptions.ConnectionString);
: new MySqlConnectionSettings(
new NamedConnectionStringResolver(options)
.ResolveConnectionString(relationalOptions.ConnectionString ?? string.Empty));

protected virtual bool Equals(MySqlOptions other)
{
Expand Down
51 changes: 36 additions & 15 deletions test/EFCore.MySql.Tests/MySqlRelationalConnectionTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Diagnostics;
using System.Transactions;
Expand All @@ -9,8 +10,10 @@
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using MySqlConnector;
using Pomelo.EntityFrameworkCore.MySql.Diagnostics.Internal;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal;
Expand All @@ -32,6 +35,19 @@ public void Creates_MySql_Server_connection_string()
Assert.IsType<MySqlConnection>(connection.DbConnection);
}

[Fact]
public void Accepts_named_connection_string()
{
using var connection = CreateConnection(
new DbContextOptionsBuilder()
.UseMySql(@"name=NamedConnectionString", AppConfig.ServerVersion)
.UseApplicationServiceProvider(
new ServiceCollection()
.AddSingleton<IConfiguration, FakeConfiguration>()
.BuildServiceProvider())
.Options);
}

[Fact]
public void Uses_DbDataSource_from_DbContextOptions()
{
Expand Down Expand Up @@ -274,7 +290,7 @@ public void CurrentAmbientTransaction_returns_transaction_with_AutoEnlist_enable
private static MySqlRelationalConnection CreateConnection(DbContextOptions options = null, DbDataSource dataSource = null)
{
options ??= new DbContextOptionsBuilder()
.UseMySql(@"Server=localhost;User ID=some_user;Password=some_password;Database=MySqlConnectionTest", AppConfig.ServerVersion)
.UseMySql(ConnectionString, AppConfig.ServerVersion)
.Options;

foreach (var extension in options.Extensions)
Expand Down Expand Up @@ -318,20 +334,7 @@ private static MySqlRelationalConnection CreateConnection(DbContextOptions optio
singletonOptions);
}

private const string ConnectionString = "Fake Connection String";

private static IDbContextOptions CreateOptions(
RelationalOptionsExtension optionsExtension = null)
{
var optionsBuilder = new DbContextOptionsBuilder();

((IDbContextOptionsBuilderInfrastructure)optionsBuilder)
.AddOrUpdateExtension(
optionsExtension
?? new FakeRelationalOptionsExtension().WithConnectionString(ConnectionString));

return optionsBuilder.Options;
}
private const string ConnectionString = @"Server=localhost;User ID=some_user;Password=some_password;Database=MySqlConnectionTest";

private class FakeDbContext : DbContext
{
Expand All @@ -344,4 +347,22 @@ public FakeDbContext(DbContextOptions<FakeDbContext> options)
{
}
}

private class FakeConfiguration : IConfiguration
{
public IConfigurationSection GetSection(string key)
=> throw new NotImplementedException();

public IEnumerable<IConfigurationSection> GetChildren()
=> throw new NotImplementedException();

public IChangeToken GetReloadToken()
=> throw new NotImplementedException();

public string this[string key]
{
get => ConnectionString;
set => throw new NotImplementedException();
}
}
}