几个项目的几个DbContext。 ASP.NET MVC EF

时间:2015-09-05 09:58:14

标签: c# asp.net-mvc entity-framework dbcontext

我将使用EF将旧项目移动到ASP.NET MVC,并且存在一个问题。

我的项目包括几个子项目,每个项目都有自己的表,尽管表结构相同。

实施例。 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语句,有没有什么好方法呢?

2 个答案:

答案 0 :(得分:1)

您需要实现工厂设计模式

  • 创建一个界面,例如将其称为IDBCustomers
  • 让所有相关的DbContext类实现它。
  • 编写一个使用switch / case的工厂类来返回正确的实现(是的,这仍然像你的if / then / else一样 - 但是这样你就做了一次并且没有将这种代码分散到整个代码库)。
  • 在您的工厂打电话给GetDBCustomers,照常进行。

详细了解工厂设计模式及其对您有利的原因。

祝你好运。

答案 1 :(得分: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();
    }

}
相关问题