title: Entity Framework change connection at runtime
body: <p>I have a web API project which references my model and DAL assemblies. The user is presented with a login screen, where he can select different databases.</p>
  
  <p>I build the connection string as follows:</p>
  
  <pre><code>    public void Connect(Database database)
      {
          //Build an SQL connection string
          SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
          {
              DataSource = database.Server,
              InitialCatalog = database.Catalog,
              UserID = database.Username,
              Password = database.Password,
          };
  
          //Build an entity framework connection string
          EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
          {
              Provider = database.Provider,
              Metadata = Settings.Default.Metadata,
              ProviderConnectionString = sqlString.ToString()
          };
      }
  </code></pre>
  
  <p>First of all, how do I actually change the connection of the data context?</p>
  
  <p>And secondly, as this is a web API project, is the connection string (set at login per above) persistent throughout the user's interaction or should it be passed every time to my data context?</p>