title: Setup Entity Framework For Dynamic Connection String
body: <p>I am working on an app that will use the same database schema across multiple databases. For this reason, I've created a database called <code>MyTemplate</code>. When a new user is created, they will have their own instance of the database. So, a database called something like <code>MyTemplate_[UserName]</code> will be created. When a user logs in, I need to point their queries to their database. For this reason, I know that I need to set the connection string at runtime. My problem is, I also want to use the Entity Framework.</p>
  
  <p>Currently, I created a new .edmx using MyTemplate as the source. I thought I would be able to update the code and set the connection string there. Unfortunately, I can't figure out how to set it. The constructor to TemplateEntities does not have an overload that allows me to pass in a connection string. I noticed that TemplateEntities derived from DbContext, I don't think this would be the problem.</p>
  
  <pre><code>string connectionString = GetUsersConnectionString();
  using (TemplateEntities entities = new TemplateEntities())
  {
    TemplateEntity entity = new TemplateEntity();
  
    // Save to the database
    entities.TemplateEntity.Add(entity);
    entities.SaveChanges();
  }
  </code></pre>
  
  <p>Am I creating the <code>.edmx</code> incorrectly? Or am I missing something entirely? Everything I Google shows an overload that should allow a connection string passed in. However, I don't have that overload available.</p>