ASP.Net Core Npgsql An operation is already in progress
<p>In order to reuse the same DbCommand and not instatiating a new connection every time I need to access the DB in the same request, I created a method that saves the DbCommand and returns it if it was previously instantiated:</p>

<pre><code>public static class InformacionInscripcionesViewModel {

        private static NpgsqlConnection _dbConnection;
        private static NpgsqlCommand _dbCommand;

        private static NpgsqlCommand GetDbCommand() {
            InformacionInscripcionesViewModel._dbConnection = InformacionInscripcionesViewModel._dbConnection ?? new NpgsqlConnection("Host=192.168.1.127;Username=siu;Password=123456;Database=guaraniprueba20160816");
            if (InformacionInscripcionesViewModel._dbConnection.State == ConnectionState.Closed)
                InformacionInscripcionesViewModel._dbConnection.Open();

            return InformacionInscripcionesViewModel._dbCommand
                   ?? (InformacionInscripcionesViewModel._dbCommand = new NpgsqlCommand {Connection = InformacionInscripcionesViewModel._dbConnection});
        }
}
</code></pre>

<p>Example of how it is used in a Model:</p>

<pre><code>var dbCommand = InformacionInscripcionesViewModel.GetDbCommand();
dbCommand.CommandText = @"SELECT sga_propuestas.nombre_abreviado AS nombre_carrera, ....";
dbCommand.ExecuteNonQuery();
</code></pre>

<p>When I make 2 simultaneous requests to a URL using it, it throws an exception at <code>dbCommand.ExecuteNonQuery();</code> with message <code>An operation is already in progress</code>.</p>

<p>What could I do to prevent this? Should <code>DbCommand</code> be instanted every time prior to use? Any light on this?</p>

<pre><code>{System.InvalidOperationException: An operation is already in progress.
   at Npgsql.NpgsqlConnector.StartUserAction(ConnectorState newState)
   at Npgsql.NpgsqlCommand.ExecuteNonQueryInternal()
   at Npgsql.NpgsqlCommand.ExecuteNonQuery()
   at SIUNPAZ.Models.InformacionInscripcionesViewModels.InformacionInscripcionesViewModel.GetTotalesInscripcionesPorMaterias(DateTime fechaDesde, DateTime fechaHasta, String carrera)
   at SIUNPAZ.Controllers.InformacionInscripcionesController.PorMaterias(DateTime fechaDesde, DateTime fechaHasta, String carrera)
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ObjectMethodExecutor.Execute(Object target, Object[] parameters)
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.&lt;InvokeActionFilterAsync&gt;d__28.MoveNext()}
</code></pre>