Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Database during runtime in Entity Framework, without changing the Connection

Tags:

I have a server that hosts 50 databases with identical schemas, and I want to start using Entity Framework in our next version.

I don't need a new connection for each of those databases. The privileges of the one connection can talk to all of the 50 databases, and for data management and speed (this is a WebAPI application) I don't want to instantiate a new EF context every time I talk to each of the databases if I don't have to, unless of course if this occurs each time a request comes to the server then no big deal.

All I really need is the ability to change the USE [databasename] command, which I assume eventually gets sent to the server from EF.

Is there a way to accomplish this in code? Does EF maintain a read/write property in the Context that refers to the database name that could be changed on the fly before calling SaveChanges(), etc.??

Thank you!!!

bob

like image 546
user2197022 Avatar asked Sep 17 '13 04:09

user2197022


People also ask

How do I change databases in Entity Framework?

When using Entity Framework, the database connection string is stored in the app. config file by default, and the entity object references that to connect to the database. If, for any reason, a user needs to change the database, he can do so simply by editing that file prior to running the program.

How does Entity Framework affect the connection with the database?

Because an open connection to the database consumes a valuable resource, the Entity Framework opens and closes the database connection only as needed. You can also explicitly open the connection. For more information, see Managing Connections and Transactions. Once in each application domain.

How do I change my database connection?

From the Explore Repository, select Tools, and then Change Database Connections. In the Type field, select a report type, then an item, and then click OK. In Change Database Connection, select the item, and then click OK. In Find all references to the Database Connection, select the database connection to change.

Does Entity Framework keep connection open?

Entity Framework will handle database connections automatically by default. Note two things here: EF will open the connection if you specify any LINQ or ObjectQuery method, and that connection won't be closed until the ObjectResult has been completely consumed or disposed.


2 Answers

Don't Work hard, work smart !!!!

MYContext localhostContext = new MYContext(); MYContext LiveContext = new MYContext(); //If your databases in different servers LiveContext.Database.Connection.ConnectionString = LiveContext.Database.Connection.ConnectionString.Replace("localhost", "Live"); //If your databases have different Names LiveContext.Database.Connection.ConnectionString = LiveContext.Database.Connection.ConnectionString.Replace("DBName-Localhost", "DBName-Live"); 

the structure for databases should be the same ;)

like image 97
Zeyad Qunees Avatar answered Nov 07 '22 23:11

Zeyad Qunees


You can take a look at:

  • SO question about passing existing SQL Connection to EntityFramework Context
  • and at this article describing how to change database on existing connection.

Please let me know if any additional help is needed.

Edited
Updated 2nd link to point to SqlConnection.ChangeDatabase method.
So eventually code would look similarly to the following:

MetadataWorkspace workspace = new MetadataWorkspace(   new string[] { "res://*/" },    new Assembly[] { Assembly.GetExecutingAssembly() });  using (SqlConnection sqlConnection = new SqlConnection(connectionString)) using (EntityConnection entityConnection = new EntityConnection(workspace, sqlConnection)) using (NorthwindEntities context = new NorthwindEntities(entityConnection)) {   // do whatever on default database   foreach (var product in context.Products)   {     Console.WriteLine(product.ProductName);   }    // switch database   sqlConnection.ChangeDatabase("Northwind");   Console.WriteLine("Database: {0}", connection.Database); } 
like image 42
alex.b Avatar answered Nov 07 '22 23:11

alex.b