title: Entity Framework : Change connection string at runtime
body: <p>Assuming there is an ASP.NET MVC application that uses Entity Framework 6 with a code-first approach and StructureMap as IoC.</p>
  <p>It also uses the Unit Of Work pattern.</p>
  <p>Domain Class:</p>
  <pre class="lang-cs prettyprint-override"><code>public class Product
  {
      public int Id { get; set; }
      public string Name { get; set; }
      public decimal Price { get; set; }
  }
  </code></pre>
  <p><code>IUnitOfWork</code> and <code>DbContext</code>:</p>
  <pre class="lang-cs prettyprint-override"><code>public interface IUnitOfWork
  {
      IDbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class;
      int SaveChanges();
  }
      
  public class Sample07Context : DbContext, IUnitOfWork
  {
      public DbSet&lt;Product&gt; Products { set; get; }
      
      #region IUnitOfWork Members
      
      public new IDbSet&lt;TEntity&gt; Set&lt;TEntity&gt;() where TEntity : class
      {
          return base.Set&lt;TEntity&gt;();
      }
      
      #endregion
  }
  </code></pre>
  <p>Business logic in service classes :</p>
  <pre class="lang-cs prettyprint-override"><code>public interface IProductService 
  {
      void AddNewProduct(Product product);
      IList&lt;Product&gt; GetAllProducts();
  }
  
  public class ProductService: IProductService 
  {
      IUnitOfWork _uow;
      IDbSet&lt;Product&gt; _products;
      
      public ProductService(IUnitOfWork uow) 
      {
          _uow = uow;
          _products = _uow.Set&lt;Product&gt;();
      }
  
      public void AddNewProduct(Product product) 
      {
          _products.Add(product);
      }
  
      public IList&lt;Product&gt; GetAllProducts() 
      {
          return _products.Include(x =&gt; x.Category).ToList();
      }
  }
  </code></pre>
  <p>Injecting the service class in controller</p>
  <pre class="lang-cs prettyprint-override"><code>public class HomeController : Controller
  {
      private IProductService _productService;
      private IUnitOfWork _uow;
          
      public HomeController(IUnitOfWork uow, IProductService productService)
      {
          _productService = productService;
          _uow = uow;
      }
          
      [HttpGet]
      public ActionResult Index()
      {
          var list = _productService.GetAllProducts();
          return View(list);
      }
  }
  </code></pre>
  <p><code>StructureMap</code> Configuration that we call in app_start :</p>
  <pre class="lang-cs prettyprint-override"><code>private static void initStructureMap()
  {
      ObjectFactory.Initialize(x =&gt;
      {
          x.For&lt;IUnitOfWork&gt;().HttpContextScoped().Use(() =&gt; new Sample07Context());
          x.ForRequestedType&lt;IProductService&gt;().TheDefaultIsConcreteType&lt;EfProductService&gt;();
      });
      
      //Set current Controller factory as StructureMapControllerFactory
      ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
  }
  </code></pre>
  <p>Everything works fine with the single database but in my scenario user can use multiple databases, I mean the user should be able to change the connection string at runtime. We create a separate database for each project that the user creates in the application.
  Now the problem is that we inject <code>DbContext</code> into the service and <code>DbContext</code> reads the connection string from the <code>web.config</code> so when user changes the database we cannot set a new connection string to the <code>DbContext</code>.</p>
  <p>What do you suggest?</p>