title: How to refactor the connectionString in my Entity Framework & ASP.NET MVC project?
body: <p>I have a large number of stored procedures to work with and I have to work with Entity Framework.</p>
  <p>I got for example this controller where I'm just calling the database to show my table:</p>
  <pre><code>public class CarguioController : Controller
  {
      public ActionResult Index(int? page)
      {
          string cs = ConfigurationManager.ConnectionStrings[&quot;DefaultConnection&quot;].ConnectionString;
  
          using (SqlConnection conn = new SqlConnection(cs))
          {
              // establece conneción
              SqlParameter param1 = new SqlParameter();
              param1.ParameterName = &quot;@MODO&quot;;
              param1.SqlDbType = SqlDbType.Int;
              param1.Value = 2;
  
              SqlCommand cmdProcedure = new SqlCommand(@&quot;Almacen.[PRC_Carguio]&quot;, conn);
              cmdProcedure.Parameters.Add(param1);
  
              conn.Open();
  
              cmdProcedure.CommandType = CommandType.StoredProcedure;
  
              SqlDataReader dr = cmdProcedure.ExecuteReader();
              List&lt;CarguioViewModel&gt; lst = new List&lt;CarguioViewModel&gt;();
  
              int pageNumber = page ?? 1;
              int pageSize = 8;
  
              if (dr.HasRows)
              {
                  while (dr.Read())
                  {
                      lst.Add(new CarguioViewModel
                      {
                          Carguio_ID = dr.GetInt32(0),
                          Vehiculos_ID = dr.GetInt32(1),
                          ManifiestoCarga_ID = dr.GetInt32(2),
                          Guia_ID = dr.GetInt32(3),
                          Programaciones_ID = dr.GetInt32(4),
                          Numero = dr.GetInt32(5),
                          NroMobil = dr.GetString(6),
                          Fecha = dr.GetDateTime(7),
                          Usuarios_ID = dr.GetInt32(8),
                          Sucursales_IS = dr.GetInt32(9)
                      });
                      //display retrieved record
                  }
  
                  return View(lst.ToPagedList(pageNumber, pageSize));
              }
              else
              {
                  Console.WriteLine(&quot;No data found.&quot;);
              }
  
              dr.Close();
              conn.Close();
          }
  
          return View();
      }
  }
  </code></pre>
  <p>As you can see, I have to connect with the SQL Server database many times. Maybe you have done a similar job with ASP.NET MVC projects or have any idea to refactor my code?</p>
  <p>I have more than 30 tables and everyone has more a Crud and other functions.</p>
  <p>I've been searching for this but there is just the same example.</p>
  <pre><code>string cs = ConfigurationManager.ConnectionStrings[&quot;DefaultConnection&quot;].ConnectionString;
  </code></pre>