切换连接字符串

发布于 2024-07-26 05:39:05 字数 1309 浏览 5 评论 0原文

我创建了一个类库,其中包含其他类必须连接到的三个数据库的三个实体模型。 然后,该类库由三个不同的 Web 服务和一个网站使用。 这些都安装在自己的服务器上,因此没有简单的方法让它们共享相同的连接数据。

在我的 web.config 文件中,有这样一行:

<connectionStrings configSource="Dependencies.ConnectionStrings.Config"/>

这将从单独的 XML 文件加载连接字符串。 它工作正常,但这也意味着我现在有这个 Dependency.ConnectionStrings.Config 文件的 5 个不同副本。 我更愿意在开发过程中使用单个文件。

但是,我的解决方案还包含四个安装项目,在这些设置中,配置文件必须彼此相邻。

那么我如何告诉 VS 在多个项目之间共享单个配置文件而不修改 web.config 呢?


To make it slightly more complicated, the class library uses an app.config to store the connection strings. And I can't tell app.config to link the connection strings to a separate config file, apparently... One additional complication is that the connection strings need to be modified by the system administration after installation. That's because it will have to look to a different database than the development databases. (Actually, during development, I use 3 databases to split up the entity logic. The Test and Production environments will just use a single database.)
My current solution is simple. I've added the Dependencies.ConnectionStrings.Config file to a separate folder in my solution and regularly drag&drop this into the other projects to update them. Although this works, it's error-prone...

I've created a class library that contains three entity models for three databases that other classes have to connect to. This class library is then used by three different web services and a website. These are all installed on their own server so there's no easy way to let them share the same connection data.

In my web.config files I have this line:

<connectionStrings configSource="Dependencies.ConnectionStrings.Config"/>

This will load the connection strings from a separate XML file. It works fine but it also means that I now have 5 different copies of this Dependencies.ConnectionStrings.Config file. I would prefer to use a single file during development.

However, my solution also contains four setup projects and in these setups, the config files must be next to each other.

So how do I tell VS to share a single config file between multiple projects yet without modifying web.config?


To make it slightly more complicated, the class library uses an app.config to store the connection strings. And I can't tell app.config to link the connection strings to a separate config file, apparently...
One additional complication is that the connection strings need to be modified by the system administration after installation. That's because it will have to look to a different database than the development databases. (Actually, during development, I use 3 databases to split up the entity logic. The Test and Production environments will just use a single database.)


My current solution is simple. I've added the Dependencies.ConnectionStrings.Config file to a separate folder in my solution and regularly drag&drop this into the other projects to update them. Although this works, it's error-prone...

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

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

发布评论

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

评论(2

浅暮の光 2024-08-02 05:39:05

最大的问题是您不能在当前主配置文件上方的文件夹层次结构中拥有链接的配置文件,这使得共享它们变得困难。

您是否考虑过将其放置在配置层次结构的更高位置 - 也许在 根 Web 中.config 还是 machine.config?

另一种选择是让一些预构建事件构建这些文件,因此您不必担心拥有它们的多个副本。 当我需要为每个开发人员创建不同的连接字符串时,我非常成功地使用了预构建选项。

The big issue is you cannot have a linked config file in the folder hierarchy above the current main config file, which makes sharing them difficult.

Have you looked at placing this somewhere higher up the configuration hierarchy- perhaps in the root web.config or machine.config?

Another option is to have some pre build events build these files, so you don't have to worry about having multiple copies of them. I've used the prebuild option quite successfully when I need to create a different connection string per developer.

剪不断理还乱 2024-08-02 05:39:05

我不知道是否可以在不修改文件的情况下执行您所要求的操作。

我同意如果你有一个可以被所有服务器使用的文件会好得多,那么你就不会有版本控制问题等。

说实话,整个设置听起来相当复杂,如果说错了可能很容易出错配置文件最终位于错误的服务器上。

我很想将连接信息集中在共享数据库中。 您可以记录每个服务器应该使用的连接字符串(根据服务器名称存储)。 这样每个服务器都会知道它应该使用哪些连接字符串。 当然,这假设您的服务器都可以联系一台中央数据库服务器(取决于您的设置)。

当然,还有其他方法可以获得所需的结果,但我认为如果您“自己动手”而不是尝试改变内置应用程序设置来执行您想要的操作,您可以获得更优雅的解决方案。

I don't know if it's possible to do what you're asking without modifying the file.

I agree it would be much better if you had one file that could be used by all servers, then you won't have versioning problems, etc.

To be honest, the whole setup sounds pretty complicated and perhaps prone to error if say the wrong config file ended up on the wrong server.

I would be tempted to centralise the connection info in a shared database. You could record the connection strings that each server is supposed to use (stored against server name). This way each server would know which connection strings it sould use. This of course assumes that your servers could all contact one central database server (depends on your setup).

Of course there are other ways of getting the desired result, but I think you could get a much more elegant solution if you "roll your own" rather than trying to bend the inbuilt app settings to do what you want.

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