30% OFF - 10th Anniversary discount on Entity Framework Extensions until December 15 with code: ZZZANNIVERSARY10
Entity Framework Change Connection String How to Change at Runtime
How to change the connection at runtime?
When multiple databases exist and you need to perform database operations on different databases based on your business logic.
StackOverflow Related Questions
Answer
*DbContext has a constructor overload that accepts the name of a connection string or a connection string itself. Implement your own version and pass it to the base constructor.
public CustomerContext( string connectionString) : base(connectionString) { }
Then simply pass the name of a configured connection string or a connection string itself when you instantiate your DbContext.
using (var context = new CustomerContext("connection string 1")) { //code here } using (var context = new CustomerContext("connection string 2")) { //code here }
ZZZ Projects