title: Changing database at runtime with MVC WebApi 2
body: <p>I want to change the connection to a database at runtime in a REST Api. I want to put a variable of the request and let the Api decide which connectionstring to use.</p>
  
  <p>For example:
  I put the variable "dbid" with the value "develop"  in the request header and send it to the Api.</p>
  
  <p>The Api sees the header and gets the correct connectionstring from the web.config.</p>
  
  <p>I have three layers (data, business, api). The data contains EntityFramework to get and set data. Like this:</p>
  
  <pre><code>public class WebsiteContext : IocDbContext, IWebsites
  {
      public DbSet&lt;Website&gt; Websites { get; set; }
  
      public IEnumerable&lt;Website&gt; GetAll()
      {
          return Websites.ToList();
      }
  }
  </code></pre>
  
  <p>(IoCDbContext.cs)</p>
  
  <pre><code>public class IocDbContext : DbContext, IDbContext
  {
      public IocDbContext() : base("develop")
      {
      }
  
      public void ChangeDatabase(string connectionString)
      {
          Database.Connection.ConnectionString= connectionString;
      }
  }
  </code></pre>
  
  <p>In the business I have a class to retrieve data from the datalayer and do some logical stuff (not needed here, but still good for the story).</p>
  
  <pre><code>public class Websites : IWebsites
  {
      private readonly Data.Interfaces.IWebsites _websiteContext;
  
      #region Constructor
  
      public Websites(Data.Interfaces.IWebsites websiteContext)
      {
          _websiteContext = websiteContext;
      }
  
      #endregion
  
      #region IWebsites implementation
  
  
      public IEnumerable&lt;Website&gt; GetWebsites()
      {
          List&lt;Data.Objects.Website&gt; websiteDtos = _websiteContext.GetAll().ToList();
  
          return websiteDtos.Select(web =&gt; web.ToModel()).ToList();
      }
  
      #endregion
  }
  
  public static class WebsiteMapper
  {
      public static Website ToModel(this Data.Objects.Website value)
      {
          if (value == null)
              return null;
  
          return new Website
          {
              Id = value.Id,
              Name = value.Name
          };
      }
  }
  </code></pre>
  
  <p>And, last but not least, the controller:</p>
  
  <pre><code>public class WebsiteController : ApiController
  {
      private readonly IWebsites _websites;
  
      public WebsiteController(IWebsites websites)
      {
          _websites = websites;
      }
  
      public IEnumerable&lt;Website&gt; GetAll()
      {
          return _websites.GetWebsites().ToList();
      }
  }
  </code></pre>
  
  <p>My Unity configuration:</p>
  
  <pre><code>public static void RegisterComponents()
      {
          var container = new UnityContainer();
  
          container.RegisterType&lt;Business.Interfaces.IWebsites, Websites&gt;();
  
          container.RegisterType&lt;IDbContext, IocDbContext&gt;();
          container.RegisterType&lt;IWebsites, WebsiteContext&gt;();
  
          // e.g. container.RegisterType&lt;ITestService, TestService&gt;();
  
          GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
          DependencyResolver.SetResolver(new UnityDependencyResolver(container));
      }
  </code></pre>
  
  <p>So as you can see the connection string with the name "develop" is used by default. This will return a website with the name "website". Now I would change the header variable "dbid" to "live". The api should see this and should get the connectionstring that corresponds with the name "live". This last part is something I am trying, but nothing works. </p>
  
  <p>This I tried:</p>
  
  <ul>
  <li>Adding session to webapi. This means I break the stateless idea of REST api: not done</li>
  <li>Statics cannot work either, because everyone could get the same connectionstring, but its user specific</li>
  <li>Google, but most of the examples don't work for me</li>
  <li>Searching StackOverflow... See previous point.</li>
  </ul>
  
  <p>This is driving me crazy! There should be a way to change the connectionstring given by a value in a request header, right?</p>