0

I have read many other post about fixing the issue for NullReferenceException when calling ConfigurationManager.ConnectionStrings as well as what to do when Unit Test is using machine.config rather than app.config but none have worked for me. Below is a list of approaches that I've tried and have been unsuccessful with so far.

  1. Add Existing Item to unit test and reference the app.config for the project you are testing against
  2. Copy the app.config from the targeted project and paste it in the unit test project
  3. Ensure the bin folder of the unit test project is copying the appropriate config file
  4. Set the properties for the unit test to copy to output as always

Below is a copy of my current config within the Unit Test project:

<configuration>
<appSettings>
    <add key="Environment" value="Development" />
</appSettings>
<connectionStrings>
<add name="Connection.Development" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDB)\***;Initial Catalog=***;Persist Security Info=True;Integrated Security=True;Pooling=False" />
</connectionStrings>
</configuration>

I am expecting the following line of code to return Connection.Development connection string but instead I get the LocalSqlServer connection string from the machine.config consistently

string connectionString = ConfigurationManager.ConnectionStrings["Connection.Development"].ConnectionString;

I am using VS2017, running .Net Core 2.0, and trying to connect to local MS SQL Db within the application. If someone could tell me how they fixed this problem for their issue it would be greatly appreciated. If you know why, please pass that information along as well.

0 Answers0