切换连接字符串
我创建了一个类库,其中包含其他类必须连接到的三个数据库的三个实体模型。 然后,该类库由三个不同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最大的问题是您不能在当前主配置文件上方的文件夹层次结构中拥有链接的配置文件,这使得共享它们变得困难。
您是否考虑过将其放置在配置层次结构的更高位置 - 也许在 根 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.
我不知道是否可以在不修改文件的情况下执行您所要求的操作。
我同意如果你有一个可以被所有服务器使用的文件会好得多,那么你就不会有版本控制问题等。
说实话,整个设置听起来相当复杂,如果说错了可能很容易出错配置文件最终位于错误的服务器上。
我很想将连接信息集中在共享数据库中。 您可以记录每个服务器应该使用的连接字符串(根据服务器名称存储)。 这样每个服务器都会知道它应该使用哪些连接字符串。 当然,这假设您的服务器都可以联系一台中央数据库服务器(取决于您的设置)。
当然,还有其他方法可以获得所需的结果,但我认为如果您“自己动手”而不是尝试改变内置应用程序设置来执行您想要的操作,您可以获得更优雅的解决方案。
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.