title: Entity Framework Multi-Tenancy and dynamic schema
body: <p>I have a problem with the Entity Framework and the DBContext with multiple schema names. It only works, if i remove the dbo.__MigrationHistory table or the specific entry.</p>
  
  <p>At First my DatabaseContext:</p>
  
  <pre><code>public class AppTenantDatabaseContext : DbContext, IServiceStatusDatabaseContext, IDbModelCacheKeyProvider
  {
      public DbSet&lt;Container&gt; Items{ get; set; }
  
      protected string _prefix;
  
      public AppTenantDatabaseContext(string prefix, string connectionStringName) : base(connectionStringName)
      {
          this.Configuration.LazyLoadingEnabled = false;
          _prefix = prefix;
          System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists&lt;AppTenantDatabaseContext&gt;());
      }
  
      public AppTenantDatabaseContext(string prefix) : base("AppFramework")
      {
          this.Configuration.LazyLoadingEnabled = false;
          _prefix = prefix;
          System.Data.Entity.Database.SetInitializer(new CreateDatabaseIfNotExists&lt;AppTenantDatabaseContext&gt;());
      }
  
      public AppTenantDatabaseContext() : this(string.Empty) { }
  
      protected override void OnModelCreating(DbModelBuilder modelBuilder)
      {
          modelBuilder.HasDefaultSchema(this._prefix);            
          modelBuilder.Entity&lt;Container&gt;().ToTable("Container", schemaName: _prefix);
          base.OnModelCreating(modelBuilder);
      }
  
      public string CacheKey
      {
          get { return this._prefix; }
      }
  }
  </code></pre>
  
  <p>If i run the DBContext an entry in the table __MigrationHistory is added for the ContextKey of the full class name with the model that contains the schema. If i want to create the tables for another schema, the code runs in a exception, because the models are different. Thats understandable.</p>
  
  <p>Now my Question:
  How do i achieve it to work with the migration history table, without deleting it?</p>
  
  <p>In my Opinion it could work if either the __MigrationHistory Table will be created foreach schema name instead of "dbo" or if the __MigrationHistory Table contains an entry foreach schema name combined with the Class name. How can i do this?</p>
  
  <p>Thanks for your help!</p>