title: Connection string Database First approach Entity Framework
body: <p>I need someone to help me with this.<br/></p>
  
  <p>I have a project developed in ASP.NET MVC. I am using database first approach. I have around 40 databases in my model. The problem I have is the following. I already did coding for two databases by calling like this in controller:</p>
  
  <pre><code>db1Entities image = new db1Entities();
  var update = image.App_Details(Convert.ToInt32(primarykey)).ToList();
  </code></pre>
  
  <p>But now I need to call this for 40 different databases. All these databases have same tables and stored procedures. I need to select connection string based on name which I have in front end. </p>
  
  <p>If I have to do this inside controller, then I need to put 40 different conditions every time and call above code like :</p>
  
  <pre><code>if(name == "test1")
  {
      db1Entities image = new db1Entities();
      var update = image.App_Details(Convert.ToInt32(primarykey)).ToList();
  }
  
  if(name == "test2")
  {
      db2Entities image = new db2Entities();
      var update = image.App_Details(Convert.ToInt32(primarykey)).ToList();
  }
  </code></pre>
  
  <p>and so on. I don't want to do this for every controller. So what I am thinking about is I could do this in separate class by comparing with names and assign to image like:</p>
  
  <pre><code>if (name == "test1")
  {
      db1Entities image = new db1Entities();              
  }
  
  if(name == "test2")
  {
      db2Entities image = new db2Entities();              
  }
  </code></pre>
  
  <p>and after this I should be able to pass this image and connect database in controller like :</p>
  
  <pre><code>   var update = image.App_Details(Convert.ToInt32(primarykey)).ToList();
  </code></pre>
  
  <p>Please can someone help me in achieving this?</p>