Skip to content

Commit 974f0ff

Browse files
committed
Fix crash in MySqlConnection.Database before connection is opened. Fixes #205
1 parent 1798bcf commit 974f0ff

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

src/MySqlConnector/MySqlClient/MySqlConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public override string ConnectionString
157157
}
158158
}
159159

160-
public override string Database => m_session.DatabaseOverride ?? m_connectionSettings.Database;
160+
public override string Database => m_session?.DatabaseOverride ?? m_connectionSettings.Database;
161161

162162
public override ConnectionState State => m_connectionState;
163163

tests/SideBySide/ConnectAsync.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ public async Task ConnectSslBadClientCertificate()
179179
}
180180
}
181181

182+
[Fact]
183+
public async Task ConnectionDatabase()
184+
{
185+
var csb = AppConfig.CreateConnectionStringBuilder();
186+
using (var connection = new MySqlConnection(csb.ConnectionString))
187+
{
188+
Assert.Equal(csb.Database, connection.Database);
189+
190+
await connection.OpenAsync();
191+
192+
Assert.Equal(csb.Database, connection.Database);
193+
Assert.Equal(csb.Database, await QueryCurrentDatabaseAsync(connection));
194+
}
195+
}
196+
182197
[SecondaryDatabaseRequiredFact]
183198
public async Task ChangeDatabase()
184199
{

tests/SideBySide/ConnectSync.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@ public void ConnectTimeout()
184184
}
185185
}
186186

187+
[Fact]
188+
public void ConnectionDatabase()
189+
{
190+
var csb = AppConfig.CreateConnectionStringBuilder();
191+
using (var connection = new MySqlConnection(csb.ConnectionString))
192+
{
193+
Assert.Equal(csb.Database, connection.Database);
194+
195+
connection.Open();
196+
197+
Assert.Equal(csb.Database, connection.Database);
198+
Assert.Equal(csb.Database, QueryCurrentDatabase(connection));
199+
}
200+
}
201+
187202
[SecondaryDatabaseRequiredFact]
188203
public void ChangeDatabase()
189204
{

0 commit comments

Comments
 (0)