2015-09-05 74 views
1

我打算將一箇舊項目移至帶有EF的ASP.NET MVC,並且存在一個問題。幾個項目的DbContext。 ASP.NET MVC EF

我的項目包含幾個子項目,每個項目都有自己的表格,儘管表格結構相同。

Ex。 XXX_Customers,YYY_Customers等

我發現了一個解決方案來實現我想要的。 (使用if-else-then語句)。

控制器動作:

public ActionResult Index(string projectname) 
    { 
      List<Customers> list = new List<Customers>() ; 

      if (projectname=="XXX") 
      { 
       list = new BaseContext().Customers.ToList(); 
      } 
      else if (projectname=="YYY") 
      { 
       list = new BaseContext2().Customers.ToList(); 
      } 
      return View(list); 
    } 

型號:

public class Customers 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string UniqueValue { get; set; } 
    } 

    public class BaseContext : DbContext 
    { 
     public BaseContext() 
      : base("myconnectionstring") 
     { 
      Configuration.LazyLoadingEnabled = false; 
     } 
     public DbSet<Customers> Customers { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Customers>().Property(p => p.Id).HasColumnName("ID"); 
      modelBuilder.Entity<Customers>().Property(p => p.Name).HasColumnName("Name"); 
      modelBuilder.Entity<Customers>().Property(p => p.UniqueValue).HasColumnName("UniqueValue"); 
      modelBuilder.Entity<Customers>().ToTable("XXX_Table1", "MyScheme"); 

     } 
    } 

    public class BaseContext2 : DbContext 
    { 
     public BaseContext2() 
      : base("myconnectionstring") 
     { 
      Configuration.LazyLoadingEnabled = false; 
     } 
     public DbSet<Customers> Customers { get; set; } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      modelBuilder.Entity<Customers>().Property(p => p.Id).HasColumnName("ID"); 
      modelBuilder.Entity<Customers>().Property(p => p.Name).HasColumnName("Name"); 
      modelBuilder.Entity<Customers>().Property(p => p.UniqueValue).HasColumnName("UniqueValue"); 
      modelBuilder.Entity<Customers>().ToTable("YYY_Table1", "MyScheme"); 

     } 
    } 

那麼,有沒有這樣做沒有的if-else-then語句什麼好辦法?

+0

也許通過在'class BaseContext:DbContext,IGenericContext'等所有上下文中定義和實現通用接口,或者使用'dynamic'關鍵字? –

回答

1

您需要的是實施工廠設計模式

  • 創建一個接口,例如稱之爲IDBCustomers
  • 使所有相關的DbContext類實現它。
  • 編寫一個工廠類,使用switch/case返回正確的實現(是的,這仍然像你的if/then/else - 但這種方式你做一次,不要在你的整個這種類型的代碼散佈代碼庫)。
  • 致電GetDBCustomers在您的工廠,並照常進行。

在網上閱讀關於工廠設計模式的更多信息以及爲什麼它對您有好處。

祝你好運。

+0

我做了一個簡單的工廠,但它不起作用。你能看到下面嗎? – John

+0

BaseContext和BaseContext2應實現您的接口並返回該客戶列表。 context1/2似乎是多餘的 –

0
public interface IDbCustomer 
{ 
    DbContext GetDbCustomer(); 
} 

public class FactoryDbCustomer 
{ 
    static public IDbCustomer GetDbContext(string projectName) 
    { 
     IDbCustomer obCustomer = null; 

     switch (projectName) 
     { 
      case "XXX": 
       obCustomer = new context1(); 
       break; 
      case "YYY": 
       obCustomer = new context2(); 
       break; 
      default: 
       break; 
     } 

     return obCustomer; 
    } 
} 

public class context1 : IDbCustomer 
{ 
    public DbContext GetDbCustomer() 
    { 
     return new BaseContext(); 
    } 

} 
public class context2 : IDbCustomer 
{ 
    public DbContext GetDbCustomer() 
    { 
     return new BaseContext2(); 
    } 

}