If you have a connection to a Database through Entity Framework and you need to switch it to another database (with the exact same structure) you just need to set the Connection.ConnectionString (as seen below).
I had an application where we created a copy of the Master database when setting up a new client. So using Entity Framework I switched from the master database to the client (depending on what the admin was doing).
using (MasterEntities aEntities = new MasterEntities()) {//Switch db connected to
aEntities.Database.Connection.ConnectionString = aEntities.Database.Connection.ConnectionString.Replace("OldDatabaseName", "NewDatabaseName"); //Some Query
}
This post first appeared on Random Technical Thoughts | Things I Want To Remem, please read the originial post: here