title: Entity Framework schema and connection change at runtime
body: <p>I need to do connection and schema changes at runtime using Entity Framework.</p>
  
  <p>I tried the code from this: <a href="https://stackoverflow.com/questions/2663164/changing-schema-name-on-runtime-entity-framework">Post</a></p>
  
  <p>I´m using that code:</p>
  
  <pre><code>        public EntityConnection SetCurrentEntityConnection(string schemaName, string databaseIPAddress, int databasePort, string userName, string password, string model)
      {
          XmlReader[] MappingReader = new XmlReader[] { XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream(model + ".msl")) };
          XmlReader[] ConceptualReader = new XmlReader[] { XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream(model + ".csdl")) };
  
          var StorageReader = XmlReader.Create(Assembly.GetExecutingAssembly().GetManifestResourceStream(model + ".ssdl"));
          XNamespace StorageNS = "http://schemas.microsoft.com/ado/2009/02/edm/ssdl";
  
          var StorageXml = XElement.Load(StorageReader);
  
          foreach (var EntitySet in StorageXml.Descendants(StorageNS + "EntitySet"))
          {
              var SchemaAttribute = EntitySet.Attributes("Schema").FirstOrDefault();
              if (SchemaAttribute != null)
                  SchemaAttribute.SetValue(schemaName);
          }
          StorageXml.CreateReader();
  
          var Workspace = new MetadataWorkspace();
          StoreItemCollection StorageCollection = new StoreItemCollection(new XmlReader[] { StorageXml.CreateReader() });
          EdmItemCollection ConceptualCollection = new EdmItemCollection(ConceptualReader);
          StorageMappingItemCollection MappingCollection = new StorageMappingItemCollection(ConceptualCollection, StorageCollection, MappingReader);
          Workspace.RegisterItemCollection(ConceptualCollection);
          Workspace.RegisterItemCollection(StorageCollection);
          Workspace.RegisterItemCollection(MappingCollection);
  
          ///
          /// Adjust the connection string
          /// 
          var ConnectionData = new EntityConnectionStringBuilder();
          ConnectionData.Provider = "Oracle.ManagedDataAccess.Client";
          ConnectionData.Metadata = "metadata=res://*/Model.Model.csdl|res://*/Model.Model.ssdl|res://*/Model.Model.msl";
          ConnectionData.Name = "Entities";
  
          /// Build the connection string (using Oracle EZ Connection string)
          string conn = "data source=" + userName + "/" + password + "@" + databaseIPAddress + ":" + databasePort + "/" + schemaName ;
  
          ConnectionData.ProviderConnectionString = conn;
  
  
          var Connection = DbProviderFactories.GetFactory(ConnectionData.Provider).CreateConnection();
          Connection.ConnectionString = ConnectionData.ProviderConnectionString;
  
          new EntityConnection(Workspace, Connection);
      }
  </code></pre>
  
  <p>I´m running two problems:</p>
  
  <p>Problem 1:
  The foreach loop cannot find any EntitySet to change schema. </p>
  
  <p>Problem 2: 
  I don´t know how to use the newly created EntityConnection in my EF Application. I normally use:</p>
  
  <pre><code>Entity dbContext = new Entity();
  </code></pre>
  
  <p>And start doing queries on my dbContext, but I don´t know how to handle the EntityConnection and what should be done with it. So, what is the relation between the newly created EntityConnection and the original DbContext.</p>
  
  <p>By the way, I´m using DatabaseFirst for Oracle. The DMDX doesn´t generate Entity constructor with connection parameters.</p>
  
  <p>Thanks for any help. This is driving me crazy...</p>