Amin Mohammadi Amin Mohammadi - 1 month ago 7
C# Question

Using multiple connection strings for the same DBContext in EntityFramework

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>

Answer

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");
Comments