2016-02-29 98 views
1

我有一個ASP.NET 5的Web API,我希望可以爲多個模型使用單個的dbcontext。這些模型指向我的數據庫中具有不同架構的表。下面實體框架7 - 單個dbcontext,不同的架構

該模型包含多個類

  • 驗證
  • 研究

簡化了一點:Auth.cs

public class MyContext : DbContext 
    { 
     public DbSet<Auth.App> Apps { get; set; } 
     public DbSet<Auth.Permission> Permissions { get; set; } 
     public DbSet<Study.StudyLink> StudyLinks { get; set; } 
    } 
    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     foreach (var entity in modelBuilder.Model.GetEntityTypes()) 
     { 
      if (entity.Name.Contains("Auth+")) 
      { 
       modelBuilder.HasDefaultSchema("auth"); // Switch to the auth schema 
       modelBuilder.Entity(entity.Name).ToTable(entity.Name.Replace("myproject.Models.Auth+", string.Empty)); 
      } 
      if (entity.Name.Contains("Study+")) 
      { 
       modelBuilder.HasDefaultSchema("study"); // Switch to the study schema 
       modelBuilder.Entity(entity.Name).ToTable(entity.Name.Replace("myproject.Models.Study+", string.Empty)); 
      } 
     } 
    } 

只需使用驗證模型,我能改變默認模式,並可以訪問表沒有問題..當我添加研究模型,th e modelBuilder.Model.GetEntityTypes()foreach調出Auth和Study模型,切換默認模式,這意味着我無法訪問Auth模式,因爲它切換到學習模式。

是否有某種方法可以在不使用HasDefaultSchema()的情況下應用模式,還是需要爲我在數據庫中使用的每個模式創建新的上下文?

感謝

回答