Fluent NHibernate with ASP.NET MVC 2 - 每个用户 1 个连接字符串的会话管理

发布于 2024-10-06 04:18:05 字数 1020 浏览 1 评论 0原文

我需要配置我的 Fluent NHibernate / ASP.NET MVC 2 应用程序,以便为每个用户使用单个连接/连接字符串。这是因为:

  1. 有多个数据库,所有数据库都具有相同的架构,用户可以选择登录。
  2. 与此 ASP.NET 应用程序协同工作的旧应用程序基于数据库供应商的平台,因此坚持使用数据库用户帐户。
  3. 试图说服 DBA 提供一个万能的用户帐户比仅仅弄清楚如何使其工作更加痛苦和耗时。

我觉得我必须解释一下我的理由,以避免出现一些“不要这样做”的答案。

所以无论如何,我首先使用单个连接字符串以正常、正确的方式设置所有内容。我使用 LLBLGen 创建的 SessionManager 来创建 SessionFactorySessionFactoryApplication_Start() 期间创建并存储。我还有一个 IHttpModule,它将 NHibernate 会话绑定到当前会话上下文。 (我忘了我是从谁的网站上得到的)。

我现在想要做的是让用户到达登录页面。他们将选择他们的数据库并输入用户名和密码。应用程序将创建一个数据库连接,将其存储在用户的会话中,NHibernate 将获取该数据库连接并使用它。

一般而言,有关执行此操作的任何提示都会非常有帮助,但实际上我需要克服最初的障碍:如果我从 SessionFactory 配置中删除数据库连接信息,它会抱怨并引发异常。如果我不从 SessionFactory 配置中删除数据库连接信息,即使使用 OpenSession(IDbConnection) 提供不同的连接,它似乎也会使用该连接字符串。另外,现在我什至不希望 NHibernate 在用户登录之前执行任何操作,我觉得我的 IHttpModule 可能会导致其中一些问题。

如果这听起来更像是语无伦次的咆哮而不是问题,我深表歉意;我已经用头撞墙有一段时间了。任何和所有关于最佳实践的建议(除了“使用一个连接字符串,白痴!”)都将受到赞赏。

I need to configure my Fluent NHibernate / ASP.NET MVC 2 application to use a single connection/connection string for each user. This is because:

  1. There are multiple databases, all with the same schema, that the user can choose to log into.
  2. The legacy application, with which this ASP.NET app works in tandem, is based on the DB vendor's platform and therefore insists on using DB user accounts.
  3. Trying to convince the DBA to provide a single do-everything user account would be even more painful and time consuming than just figuring out how to make it work.

I feel I have to explain my reasons in order to avoid several "Don't do that" answers.

So anyway, I first set everything up the normal, correct way with a single connection string. I used an LLBLGen-created SessionManager to create a SessionFactory. The SessionFactory gets created and stored during Application_Start(). I also have an IHttpModule that binds the NHibernate session to the current session context. (I forget whose website I got that from).

What I now want to do is have the users arrive at a login page. They will choose their database and enter a username and password. The application will create a database connection, store it in the user's session, and NHibernate will pick up that database connection and use it.

Any tips about doing this in general would be very helpful, but really I need to just get over the initial hurdles: If I remove the DB connection info from the SessionFactory configuration, it complains and throws an exception. If I don't remove the DB connection info from the SessionFactory configuration, it seems to use that connection string even when provided a different connection using OpenSession(IDbConnection). Also, now that I don't even want NHibernate to do anything until the user has logged in, I feel like my IHttpModule may be causing some of these problems.

Apologies if this sounds more like an incoherent rant than a question; I've just been banging my head against the wall for a while now. Any and all suggestions on best practices (other than "Use one connection string, idiot!") are appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文