First of all, let me state I'm very new to EF. With that said, here's my dilemma:
首先,让我说我对EF很新。话虽如此,这是我的困境:
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.
将有一个ASP.NET应用程序迁移到ASP.NET MVC。我想为此使用EF。有一个主数据库存储“客户信息”。除此之外,每个“客户”都有自己的数据库。这些是我们的约束。
Currently, client information in the main DB that enables me to build a connection string per client and make individual SQL calls.
目前,主数据库中的客户端信息使我能够为每个客户端构建连接字符串并进行单独的SQL调用。
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.
我如何在Entity Framework中完成同样的事情?每个数据库都具有相同的模式。有没有办法以编程方式切换连接字符串?这些数据库当前位于同一台服务器上,但这不是必需的,它可能是完全不同的服务器。
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.
Web.config中的多个连接字符串将是最后的手段。即便如此,我也不确定如何准确地将其连接起来。
Thank you in advance.
先谢谢你。
3 个解决方案
#1
8
If you work through an EntityConnection in the constructor of your entities object, you can change the database pretty easily.
如果在实体对象的构造函数中使用EntityConnection,则可以非常轻松地更改数据库。
EntityConnection con = new EntityConnection(connString);
con.ChangeDatabase(dbName);
using (Entities context = new Entities(con))
{
// Some code here
}
#2
5
When you build a data context, here's how to programmatically change the connection string at runtime by modifying the Context.Connection property:
构建数据上下文时,以下是如何通过修改Context.Connection属性以编程方式在运行时更改连接字符串:
//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
摘自:http://sivapinnaka.spaces.live.com/blog/cns!B027EF7E7070AD69!211.entry
#3
2
If the number of your customers is limited and the connection strings hardly ever change, an elegant way might be to use ConfigurationManager.ConnectionStrings to retreive the connection string needed.
如果您的客户数量有限且连接字符串几乎没有变化,那么优雅的方法可能是使用ConfigurationManager.ConnectionStrings来检索所需的连接字符串。
Like
喜欢
string connectionString = ConfigurationManager.ConnectionStrings["Miller"].ConnectionString;
return new Entities(connectionString);
See also http://msdn.microsoft.com/en-us/library/ms254494.aspx
另请参见http://msdn.microsoft.com/en-us/library/ms254494.aspx
#1
8
If you work through an EntityConnection in the constructor of your entities object, you can change the database pretty easily.
如果在实体对象的构造函数中使用EntityConnection,则可以非常轻松地更改数据库。
EntityConnection con = new EntityConnection(connString);
con.ChangeDatabase(dbName);
using (Entities context = new Entities(con))
{
// Some code here
}
#2
5
When you build a data context, here's how to programmatically change the connection string at runtime by modifying the Context.Connection property:
构建数据上下文时,以下是如何通过修改Context.Connection属性以编程方式在运行时更改连接字符串:
//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
摘自:http://sivapinnaka.spaces.live.com/blog/cns!B027EF7E7070AD69!211.entry
#3
2
If the number of your customers is limited and the connection strings hardly ever change, an elegant way might be to use ConfigurationManager.ConnectionStrings to retreive the connection string needed.
如果您的客户数量有限且连接字符串几乎没有变化,那么优雅的方法可能是使用ConfigurationManager.ConnectionStrings来检索所需的连接字符串。
Like
喜欢
string connectionString = ConfigurationManager.ConnectionStrings["Miller"].ConnectionString;
return new Entities(connectionString);
See also http://msdn.microsoft.com/en-us/library/ms254494.aspx
另请参见http://msdn.microsoft.com/en-us/library/ms254494.aspx