gpt4 book ai didi

entity-framework - 如何在 Entity Framework 中为不同用户使用不同的连接字符串

转载 作者:行者123 更新时间:2023-12-04 07:22:11 27 4
gpt4 key购买 nike

我有一个要求,我需要为不同的用户使用不同的连接字符串。这个想法是让在登录屏幕上提供的 usernamepassword 用作连接字符串的 usernamepassword。从而使应用程序为不同的用户使用不同的连接字符串,并在整个应用程序中使用这个连接字符串。

如何在 EF 4.1 中获得此设置

PS:我正在使用 DbContext

最佳答案

感谢凯文荣汉斯

我就是这样做的。

在模型上下文类中

public class MyEntities : DbContext
{
public MyEntities (string connectionString)
: base(connectionString)
{
}

然后在登录 Controller 中
var dataConnection = WebConfigurationManager.OpenWebConfiguration("/").ConnectionStrings.ConnectionStrings["MyConnectionString"].ConnectionString;
dataConnection = dataConnection.Substring(0, dataConnection.LastIndexOf("\"")) + ";USER ID=" + userName +";Password=" + password + "\"";
Session["connectionString"] = dataConnection;

还有其他地方
var _db = new MyEntities (Session["connectionString"].ToString());

关于entity-framework - 如何在 Entity Framework 中为不同用户使用不同的连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14480646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com