在EntityFramework中为同一个DBContext使用多个连接字符串

时间:2016-11-01 05:09:06

标签: c# entity-framework dbcontext

我正在使用EntityFramework开发项目,我需要从默认数据库的不同数据库中获取模型数据的一部分。如何使用具有相同DbContext的多个连接字符串?

public class HMVoltageDBContext : DbContext, IDisposable// IdentityDbContext<User>, IDisposable
    {
        static HMVoltageDBContext()
        {
            Database.SetInitializer<HMVoltageDBContext>(new HMVoltageDatabaseInitializer());
        }

        public HMVoltageDBContext() : base(nameOrConnectionString: "HMVoltageDB") {
            Maps = base.Set<Map>();           
            MapSteps = base.Set<MapStep>();
            MapParents = base.Set<MapParents>();
            MapTypes = base.Set<MapType>();
            PageSizes = base.Set<PageSize>();          
            PreparationPlaces = base.Set<PreparationPlace>();
            Products = base.Set<Product>();
            ProductCategories = base.Set<ProductCategory>();            
            Statuses = base.Set<Status>();
            Steps = base.Set<Step>();
            Sets = base.Set<Set>();
            MapReceivers = base.Set<MapReceiver>();
            Receivers = base.Set<Receiver>();

        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Use singular table names
            modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();           
            modelBuilder.Configurations.Add(new MapConfiguration());
            modelBuilder.Configurations.Add(new MapParentsConfiguration());
            modelBuilder.Configurations.Add(new MapStepConfiguration());
            modelBuilder.Configurations.Add(new MapTypeConfiguration());           
            modelBuilder.Configurations.Add(new PageSizeConfiguration());
            modelBuilder.Configurations.Add(new PreparationPlaceConfiguration());
            modelBuilder.Configurations.Add(new ProductConfiguration());
            modelBuilder.Configurations.Add(new ProductCategoryConfiguration());
            modelBuilder.Configurations.Add(new StatusConfiguration());
            modelBuilder.Configurations.Add(new StepConfiguration());
            modelBuilder.Configurations.Add(new SetConfiguration());
            modelBuilder.Configurations.Add(new MapReceiverConfiguration());
            modelBuilder.Configurations.Add(new ReceiverConfiguration());

        }
        public DbSet<Map> Maps { get; set; }
        public DbSet<MapParents> MapParents { get; set; }
        public DbSet<MapReceiver> MapReceivers { get; set; }
        public DbSet<MapStep> MapSteps { get; set; }
        public DbSet<MapType> MapTypes { get; set; }
        public DbSet<PageSize> PageSizes { get; set; }
        public DbSet<PreparationPlace> PreparationPlaces { get; set; }
        public DbSet<Product> Products { get; set; }
        public DbSet<ProductCategory> ProductCategories { get; set; }
        public DbSet<Receiver> Receivers { get; set; }
        public DbSet<Status> Statuses { get; set; }
        public DbSet<Set> Sets { get; set; }
        public DbSet<Step> Steps { get; set; }
        public static HMVoltageDBContext Create()
        {
            return new HMVoltageDBContext();
        }
        public static void init()
        {
            try
            {
                Create().Database.Initialize(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }

这是我的连接字符串:

<connectionStrings>
    <add name="HMVoltageDB" connectionString="Data Source=.; Database = HMV; User Id=sa; Password=123456; " providerName="System.Data.SqlClient"/>
  </connectionStrings>

1 个答案:

答案 0 :(得分:6)

您可以使用连接字符串的名称重载DbContext的构造函数,如下所示。

public class HMVoltageDBContext : DbContext
    {
        public HMVoltageDBContext(string nameOrConnectionString) : base(nameOrConnectionString)
        {

        }
    }

之后,您可以在实例化DbContext时传递已配置的连接字符串或连接字符串的名称。

var context = new HMVoltageDBContext( "you-can-change-conn-at-runtime-here");