4

I'm working on a project using EntityFramework which I need to get part of my model's data from a different database than the default database. How can I use multiple connection strings with the same 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;
            }
        }
    }

and this my connection string:

<connectionStrings>
    <add name="HMVoltageDB" connectionString="Data Source=.; Database = HMV; User Id=sa; Password=123456; " providerName="System.Data.SqlClient"/>
  </connectionStrings>
1
  • Why not use 2 different ones? Commented Nov 1, 2016 at 5:25

1 Answer 1

8

You can overload the DbContext's constructor with the name of a connection string as shown below.

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

        }
    }

After that you can pass the name of a configured connection string or a connection string when you instantiate your DbContext.

var context = new HMVoltageDBContext( "you-can-change-conn-at-runtime-here");
Sign up to request clarification or add additional context in comments.

2 Comments

What to do with the static HMVoltageDBContext() method?
@JeroenHeier He has to configure it as shown on my post above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.