Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to switch Entity Framework database connected to

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

}

[tweetmeme only_single=”false”]



This post first appeared on Random Technical Thoughts | Things I Want To Remem, please read the originial post: here

Share the post

How to switch Entity Framework database connected to

×

Subscribe to Random Technical Thoughts | Things I Want To Remem

Get updates delivered right to your inbox!

Thank you for your subscription

×