2015-09-05 76 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(); 
    } 

}