Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework to multiple databases (same schema) at runtime?

First of all, let me state I'm very new to EF. With that said, here's my dilemma:

There will be an ASP.NET App migrated to ASP.NET MVC. I would like to utilize EF for this. There is one main database which stores "client information". Apart from that, every "client" has their own database. These are the constraints we have.

Currently, client information in the main DB that enables me to build a connection string per client and make individual SQL calls.

How would I accomplish the same thing in Entity Framework? Each database WILL have the same schema. Is there a way to programmatically switch the Connection String? These DBs are currently on the same server, but that's not a requirement and it may be a completely different server.

Any ideas?

Multiple connection strings in the Web.config would be a last resort. Even then, I'm not sure how exactly to wire this up.

Thank you in advance.

like image 451
Kevin Avatar asked Aug 10 '09 20:08

Kevin


People also ask

Can a schema have multiple databases?

In the Oracle database system, the term database schema, which is also known as "SQL schema," has a different meaning. Here, a database can have multiple schemas (or “schemata,” if you're feeling fancy). Each one contains all the objects created by a specific database user.

Can we use multiple database in Entity Framework?

Multiple DbContext was first introduced in Entity Framework 6.0. Multiple context classes may belong to a single database or two different databases.

Is it possible to connect to multiple databases in Java?

To connect to multiple databases in a single JDBC program you need to connect to the two (or more) databases simultaneously using the above steps. Here, in this example, we are trying to connect to Oracle and MySQL Databases where following are the URLs and sample user credentials of both databases.

Is entity framework good for large database?

There are plenty of ways to improve performace of entity framrework with large dataset. It will resolve you major pain area and the section where you cannot see the performance improvement use classic DB stored procedure and call them with entity framework.


2 Answers

If you work through an EntityConnection in the constructor of your entities object, you can change the database pretty easily.

EntityConnection con = new EntityConnection(connString);
con.ChangeDatabase(dbName);
using (Entities context = new Entities(con))
{
    // Some code here
}
like image 118
Jacob Proffitt Avatar answered Oct 14 '22 16:10

Jacob Proffitt


When you build a data context, here's how to programmatically change the connection string at runtime by modifying the Context.Connection property:

//Get the connection string from app.config and assign it to sqlconnection string builder
SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(((EntityConnection)context.Connection).StoreConnection.ConnectionString);
sb.IntegratedSecurity = false;
sb.UserID ="User1";
sb.Password = "Password1";

//set the object context connection string back from string builder. This will assign modified connection string.
((EntityConnection)context.Connection).StoreConnection.ConnectionString = sb.ConnectionString; 

Taken from: http://sivapinnaka.spaces.live.com/blog/cns!B027EF7E7070AD69!211.entry

like image 5
Brandon Avatar answered Oct 14 '22 14:10

Brandon