在 LINQ to SQL 中动态设置数据库名称
我正在使用 LINQ to SQL 从我的应用程序连接到数据库。当我将环境从生产更改为暂存时,我可以更新 web.config 中的连接字符串。 但当环境发生变化时,我还需要更新一个值。那是数据库名称。在 LINQ to SQL 设计器文件中,数据库名称被作为属性提及,例如 -
[System.Data.Linq.Mapping.DatabaseAttribute(Name="somedbname")]
如何从某些配置文件动态获取名称值?
非常感谢任何帮助。
I am using LINQ to SQL to connect to database from my application. When I am changing environment from production to staging, I can update my connection string in web.config.
But there is one more value I need to update when environment changes. That is database name. In LINQ to SQL designer file, database name is mentioned as attribute like-
[System.Data.Linq.Mapping.DatabaseAttribute(Name="somedbname")]
How can I pick up value of Name dynamically from some config file?
Any help is really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如上面提到的
http://msdn.microsoft.com /en-us/library/system.data.linq.mapping.databaseattribute.name.aspx
“仅当连接本身未指定数据库名称时才使用 DatabaseName。”
所以你可以删除这个属性,一切都会正常工作!
as mentioned on the
http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.databaseattribute.name.aspx
"The DatabaseName is used only if the connection itself does not specify the database name."
so you can delete this attribute and all is gonna work fine!
我使用了一个包装类来提供上下文
I've used a wrapper class to provide a context along the lines of
我通过在 Visual Studio 之外编辑 .dbml 文件(设计器似乎不允许访问 DatabaseAttribute)并在此处删除 name 属性来解决此问题:(
请注意,接受的答案不再正确:此属性覆盖我的连接字符串)
I fixed this problem by editing the .dbml file outside of Visual Studio (the designer doesn't seem to allow access to the DatabaseAttribute) and getting rid of the name property here:
(note that the accepted answer is no longer correct: this attribute was overriding my connection string)