title: ConnectionFactory creating too many connections from .net core registered service
body: <p>I have inserted a connection factory based LinqToDB service in the .Net Core.</p>
  
  <p>Startup.cs </p>
  
  <pre><code>services.AddScoped&lt;IDatabase, Database&gt;( provider =&gt;
                  {
                      IConfigurationSection sqlConfig = Configuration.GetSection("Database");
                      IDataProvider dataProvider = new SqlServerDataProvider("SqlServer", SqlServerVersion.v2008);
  
                      return new Database(dataProvider,
                          new WorkspaceConnectionFactory(
                          sqlConfig["clientId"],
                          sqlConfig["clientSecret"],
                          sqlConfig["authority"],
                          sqlConfig["target"],
                          sqlConfig["connectionString"]).createConnection());
                  });
  </code></pre>
  
  <p>with this implementation of factory</p>
  
  <pre><code>namespace Workspace.Common.LinqToDB
  {
      public class WorkspaceConnectionFactory
      {
          private ClientCredential ClientCredential;
          private string Authority;
          private string Target;
          private string ConnectionString;
  
          public WorkspaceConnectionFactory(string clientId, string clientSecret, string authority, string target, string connectionString)
          {
              ClientCredential = new ClientCredential(clientId, clientSecret);
              Authority = authority;
              Target = target;
              ConnectionString = connectionString;
          }
  
          public IDbConnection createConnection()
          {
              AuthenticationContext authenticationContext = new AuthenticationContext(Authority);
              AuthenticationResult authenticationResult = authenticationContext.AcquireTokenAsync(Target, ClientCredential).Result;
              SqlConnection WorkspaceDataConnection = new SqlConnection(ConnectionString);
              WorkspaceDataConnection.AccessToken = authenticationResult.AccessToken;
  
              return WorkspaceDataConnection;
          }
      }
  }
  </code></pre>
  
  <p>After successful connection and a few queries, I get the following exception:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException (0x80131904): Resource ID : 1. The request limit for the database is 90 and has been reached. See '<a href="http://go.microsoft.com/fwlink/?LinkId=267637" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=267637</a>' for assistance.</p>
    
    <p>at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action<code>1 wrapCloseInAction)<br>
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)
       at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
       at System.Data.SqlClient.SqlDataReader.get_MetaData()
       at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
       at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task&amp; task, Boolean asyncWrite, SqlDataReader ds)
       at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
       at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
       at LinqToDB.Data.DataConnection.ExecuteReader(CommandBehavior commandBehavior) in C:\projects\linq2db\Source\LinqToDB\Data\DataConnection.cs:line 1227
       at LinqToDB.Linq.QueryRunner.ExecuteQuery[T](Query query, IDataContext dataContext, Mapper</code>1 mapper, Expression expression, Object[] ps, Int32 queryNumber)+MoveNext() in C:\projects\linq2db\Source\LinqToDB\Linq\QueryRunner.cs:line 320
       at LinqToDB.Linq.Builder.TableBuilder.AssociatedTableContext.SubQueryHelper<code>1.ExecuteSubQuery(IQueryRunner queryRunner, Object parentObject, Func</code>3 queryReader)+MoveNext() in C:\projects\linq2db\Source\LinqToDB\Linq\Builder\TableBuilder.AssociatedTableContext.cs:line 460
       at System.Collections.Generic.List<code>1.AddEnumerable(IEnumerable</code>1 enumerable)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable<code>1 source)
       at lambda_method(Closure , IQueryRunner , IDataReader )
       at LinqToDB.Linq.QueryRunner.Mapper</code>1.Map(IQueryRunner queryRunner, IDataReader dataReader) in C:\projects\linq2db\Source\LinqToDB\Linq\QueryRunner.cs:line 56
       at LinqToDB.Linq.QueryRunner.ExecuteQuery[T](Query query, IDataContext dataContext, Mapper<code>1 mapper, Expression expression, Object[] ps, Int32 queryNumber)+MoveNext() in C:\projects\linq2db\Source\LinqToDB\Linq\QueryRunner.cs:line 324
       at System.Linq.Enumerable.WhereEnumerableIterator</code>1.MoveNext()
       at System.Linq.Enumerable.UnionIterator<code>1.MoveNext()
       at System.Linq.Enumerable.SelectEnumerableIterator</code>2.MoveNext()
       at System.Linq.Set<code>1.UnionWith(IEnumerable</code>1 other)
       at System.Linq.Enumerable.DistinctIterator<code>1.FillSet()
       at System.Linq.Enumerable.DistinctIterator</code>1.ToArray()</p>
    
    <p>.........................
     at Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor.TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
       at System.Threading.Tasks.ValueTask`1.get_Result()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State&amp; next, Scope&amp; scope, Object&amp; state, Boolean&amp; isCompleted)
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
       at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
       at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
       at Workspace.API.Middlewares.AddUserHeadersMiddleware.InvokeAsync(HttpContext context, IWorkspaceRepository workspaceRepository) in C:\TFS\AT_NCSWorkspace\WorkSpaceAPI\src\API\Middlewares\AddUserHeadersMiddleware.cs:line 47
       at Workspace.API.Middlewares.ErrorLoggingMiddleware.InvokeAsync(HttpContext context) in C:\TFS\AT_NCSWorkspace\WorkSpaceAPI\src\API\Middlewares\ErrorLoggingMiddleware.cs:line 31
    ClientConnectionId:3c0ec020-5be1-4bda-adc1-205038682b08```</p>
  </blockquote>
  
  <p><strong>Steps to reproduce</strong></p>
  
  <p>Include a complete code listing (or project/solution) that we can run to reproduce the issue.</p>
  
  <p>Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.</p>
  
  <pre class="lang-cs prettyprint-override"><code>&lt;code with error and mapping classes&gt;
  </code></pre>
  
  <p><strong>Environment details</strong></p>
  
  <ul>
  <li>linq2db version: 2.6.4 with System.Data.SqlClient 4.6.1 </li>
  <li>Database Server: Azure SqlServer</li>
  <li>Database Provider: SqlServer</li>
  <li>Operating system: Windows</li>
  <li>.NET Framework: .net Core 2.2</li>
  </ul>