When trying to open a solution which contains a large number of connection strings, Visual Studio 2015 attempts to connect to each and every one when loading the project.
Each developer on our team uses a local instance of SQL Server during development. This instance can have multiple copies of our main database which include different levels of migrations - we're a small team, so often end up switching tasks half-way through.
To allow for this, we have a number of connection strings which are machine-specific, and when creating our DbContext we use the machine name to determine which connection string to use:
<connectionStrings>
<!-- Steve -->
<add name="MachineConnection_LT4" providerName="System.Data.SqlClient"
connectionString="Data Source=LT4\SQL2012;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True" />
<!-- Sean -->
<add name="MachineConnection_DESKTOP-UQV58RL" providerName="System.Data.SqlClient"
connectionString="Data Source=DESKTOP-UQV58RL\SQLEXPRESS;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True" />
<!-- Sarah -->
<add name="MachineConnection_Dev-3" providerName="System.Data.SqlClient"
connectionString="Data Source=Dev-3\;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True" />
<!-- Graham -->
<add name="MachineConnection_lt5" providerName="System.Data.SqlClient"
connectionString="Data Source=.;Initial Catalog=xxx;Integrated Security=True;MultipleActiveResultSets=True" />
<add name="MachineConnection_graham-surface3" providerName="System.Data.SqlClient"
connectionString="Data Source=.;Initial Catalog=xxx;Integrated Security=True;MultipleActiveResultSets=True" />
<add name="MachineConnection_graham-pc-10" providerName="System.Data.SqlClient"
connectionString="Data Source=.;Initial Catalog=xxx;Integrated Security=True;MultipleActiveResultSets=True" />
<!-- Alex -->
<add name="MachineConnection_Dev9" providerName="System.Data.SqlClient"
connectionString="Data Source=Dev9;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True" />
<!-- Reuben -->
<add name="MachineConnection_ReubenPC" providerName="System.Data.SqlClient"
connectionString="Data Source=REUBENPC\SQLEXPRESS;Initial Catalog=xxx;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
Our context is then initialised like so:
public class TTDataContext : DbContext
{
public const string CacheKey = "dbContext";
public TTDataContext()
: base(SqlConnections.GetConnectionStringName())
{
}
...
}
And uses this to help:
public class SqlConnections
{
private const string DefaultConnectionStringName = "DefaultConnection";
/// <summary>
/// Get the name of the connection string to use.
/// This attempts to find a machine-specific connection string e.g. MachineConnection_LT4, and falls back to
/// the default connection string if a machine-specific connection string is not found
/// </summary>
/// <returns></returns>
public static string GetConnectionStringName()
{
// This enables a connection string to be completely overridden in the cloud service configuration
try
{
var cloudConnectionString = CloudConfigurationManager.GetSetting("TTDatabaseConnectionString");
if (!String.IsNullOrEmpty(cloudConnectionString)) return cloudConnectionString;
}
catch
{
// Deliberately empty - an exception will be thrown if not running on AppFabric
}
string machineSpecificConnectionStringName = string.Format("MachineConnection_{0}", Environment.MachineName);
string connectionString = ConfigurationManager.ConnectionStrings[machineSpecificConnectionStringName] == null
? DefaultConnectionStringName
: machineSpecificConnectionStringName;
return connectionString;
}
}
When Visual Studio 2015 loads the project (either initally, or on changing Git branch), it tries to establish connections to every single connection string specified in the list (confirmed by removing all but 1 of them) and as they're all local to each relevant machine it stops responding until the connection times out, throwing this error:
Visual Studio 2013 had no issue with this set-up. Is there a way to persuade Visual Studio 2015 to behave in the same way?
This was caused by an extension - specifically the Karma Test Adapter (version 1.1.3) which seems to automatically run some code on project load.
Disabling this extension solved the issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With