Sye*_*tar 3 entity-framework entity-framework-4.1 asp.net-mvc-4
我有一个要求,我需要为不同的用户提供不同的连接字符串.我们的想法是为具有username和password在登录屏幕上提供给被用作username和password连接字符串的.从而使应用程序为不同的用户使用不同的连接字符串,并在整个应用程序中使用此连接字符串.
如何在EF 4.1中进行此设置
PS:我正在使用 DbContext
感谢Kevin Junghans
这就是我做到的.
在模型上下文类中
public class MyEntities : DbContext
{
public MyEntities (string connectionString)
: base(connectionString)
{
}
Run Code Online (Sandbox Code Playgroud)
然后在登录控制器中
var dataConnection = WebConfigurationManager.OpenWebConfiguration("/").ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString;
dataConnection = dataConnection.Substring(0, dataConnection.LastIndexOf("\"")) + ";USER ID=" + userName +";Password=" + password + "\"";
Session["connectionString"] = dataConnection;
Run Code Online (Sandbox Code Playgroud)
和其他地方
var _db = new MyEntities (Session["connectionString"].ToString());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2459 次 |
| 最近记录: |