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
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.
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.
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.
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.
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 ;)
You can take a look at:
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With