title: Dependency Injection IDbConnection SqlConnection
body: <p>I have this exception raised <strong>sometimes</strong>:</p>
  
  <pre><code>System.InvalidOperationException: 'The ConnectionString property has not been initialized.'
  </code></pre>
  
  <p>I use the built-in dependency injection:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddTransient&lt;IDbConnection&gt;(db =&gt; new SqlConnection(
                      Configuration.GetConnectionString("AppConnectionString")));
  
      services.AddScoped&lt;IAppConfigurationRepository, AppConfigurationRepository&gt;();
      services.AddScoped&lt;IHomeworkingRequestRepository, HomeworkingRequestRepository&gt;();
      services.AddScoped&lt;IEmployeeRepository, EmployeeRepository&gt;();
      services.AddScoped&lt;IEmployeeService, EmployeeService&gt;();
      services.AddScoped&lt;IHomeworkingRequestService, HomeworkingRequestService&gt;();
      services.AddMvc();
  }
  </code></pre>
  
  <p>Before, I had already this error. The code <code>services.AddScoped&lt;IDbConnection&gt;</code> I changed to <code>services.AddTransient&lt;IDbConnection&gt;</code> and solved the issue. But now I have the error again.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Please find the code when the exception occurs :</p>
  
  <pre><code>public class EmployeeRepository : IEmployeeRepository
  {
      private readonly IDbConnection _connection;
  
      public EmployeeRepository(IDbConnection connection)
      {
          _connection = connection;
      }
  
      public IEnumerable&lt;Employee&gt; GetAllActiveEmployees()
      {
          string query = @"
             SELECT
                 FirstName
                ,LastName
                ,BusinessUnit
            FROM Employees";
  
          using (var db = _connection)
          {
              _connection.Open(); // &lt;-- The exception is thrown here
              return db.Query&lt;Employee&gt;(query);
          }
      }
   }
  </code></pre>
  
  <p>Please also find the full stacktrace :</p>
  
  <pre><code>at System.Data.SqlClient.SqlConnection.PermissionDemand()
     at System.Data.SqlClient.SqlConnectionFactory.PermissionDemand(DbConnection outerConnection)
     at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
     at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
     at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
     at System.Data.SqlClient.SqlConnection.Open()
     at Homeworking.Dal.EmployeeRepository.GetAllActiveEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Repository\EmployeeRepository.cs:line 42
     at Homeworking.Service.EmployeeService.GetAllEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Service\EmployeeService.cs:line 22
     at Homeworking.Service.HomeworkingRequestService.GetAllEmployees() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Service\HomeworkingRequestService.cs:line 23
     at Homeworking.Web.Controllers.AppController.Index() in C:\Users\florian.nouri\source\repos\Homeworking\Homeworking.Web\Controllers\AppController.cs:line 22
     at lambda_method(Closure , Object , Object[] )
     at Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
     at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
     at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
  </code></pre>