更新时间:2023-01-15 20:26:22
这是因为你的首页
动作和详细信息
行动并不在同一个控制器实例执行。你可以把数据库名在会话变量并用它来创建的DbContext
实例。
That is because your Index
action and Details
action do not execute on the same controller instance. You can keep the database name in a session variable and use that to create the DbContext
instance.
public class UsersController : Controller
{
private UsersContext db;
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
if (Session["Database"] == null)
db = new UsersContext("dbA");
else
db = new UsersContext((string)Session["Database"]);
}
public ViewResult Index()
{
if (...) {
db = new UsersContext("dbA");
Session
else
{
db = new UsersContext("dbB");
Session["Database"] = "dbB";
}
return View(db.Users.ToList());
}
}