Castle ActiveRecord 更改正在使用的数据库

发布于 2024-10-15 07:31:33 字数 704 浏览 3 评论 0原文

我正在创建一个项目,其中将有一个主数据库(其中有一些全局的东西)和文件(基本上是 sqlite 文件)。

显然,当用户单击“打开”并选择一个新文件时,我希望应用程序将加载这个新数据库。

实际上我设法使用 2 个配置文件 http://www .darkside.co.za/archive/2008/01/21/castle-activerecord-connecting-to-multiple-databases.aspx

但是,有两个问题:首先,密码在xml文件中是明确的,而这个对于全局事物来说可能没问题,对于用户创建的文件来说不太好(我仍然不知道这是否是一个真正的问题,我必须询问他们是否想要他们的文件的密码)。

另一个是每个文件的连接字符串都不同(是的,我必须更改路径!),所以我可以通过两种方式工作:

  • 创建数据库的本地副本,当用户按“保存”时,数据库将复制到旧的
  • 找到一种方法来更改连接字符串或“在运行时”加载配置

我期待 DifferentDatabaseScope,但我不明白:如果我使用它,我在哪里指定所有 NHibernate 配置? (我只需传递一个连接对象!!!)

I'm creating a project where there will be a main database (where are some global things) and files (which are basically sqlite files).

Obviusly when the user clicks "open" and selects a new file, I would like that the application will load this new database.

Actually I managed to work with 2 configuration files
http://www.darkside.co.za/archive/2008/01/21/castle-activerecord-connecting-to-multiple-databases.aspx

However, there are 2 problems: first, password is clear in xml file, while this could be ok for global things, it's not very good for files created by the user (I still don't know if this is a real problem, I have to ask if they want passwords for their files).

the otherone is that connection strings will be different for each file (yes I have to change the path!), so I can work in 2 ways:

  • Create a local copy of the db and when the user press "save", the database will be copied over the older one
  • Find a way to change the connection string or load a configuration "on runtime"

I'm looking forward DifferentDatabaseScope, but I don't understand: where i specify all NHibernate configuration if I use this? (I have to pass only a connection object!!!)

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2024-10-22 07:31:33

除了在 castle activerecord 启动时之外,没有直接的方法可以更改正在使用的数据库,这就是我发现的。

There isn't a direct way to change database in use except at castle activerecord start, that's what I found.

终止放荡 2024-10-22 07:31:33

NHibernate 配置是相同的,您将使用相同的 ActiveRecord 类来执行查询,只需将用于获取记录的代码包装在 using DifferentDatabaseScope

Using DifferentDatabaseScope

另一个使用的选项是 DifferentDatabaseScope班级。该类在构造函数中采用需要打开的 IDbConnection 对象,并利用此数据库连接来尝试检索数据。

目前(在 RC3 版本中),有一条评论指出这不是防弹的。我确信他们只是在评论中省略了“还”。

在代码中执行此操作的一个示例是:

IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=TestB;Integrated Security=SSPI");

connection.Open();

using (new DifferentDatabaseScope(connection))

{

    TestTableDatabaseA test3 = TestTableDatabaseA.Find(1);

    Console.WriteLine(test3.Title);

}

The NHibernate configuration would be the same, you would use the same ActiveRecord class to do the query, just wrap the code you use to get the record in a using DifferentDatabaseScope

Using DifferentDatabaseScope

Another option to use is the DifferentDatabaseScope class. This class takes an IDbConnection object in the constructor which needs to be open, and makes use of this database connection to attempt to retrieve the data.

At present (in the RC3 build), there is a comment which notes that this isn't bullet proof. I'm sure they just left off the "yet" in the comments.

An example of doing this in code is:

IDbConnection connection = new SqlConnection("Data Source=.;Initial Catalog=TestB;Integrated Security=SSPI");

connection.Open();

using (new DifferentDatabaseScope(connection))

{

    TestTableDatabaseA test3 = TestTableDatabaseA.Find(1);

    Console.WriteLine(test3.Title);

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文