在 LINQ to SQL 中动态设置数据库名称

发布于 2024-08-04 06:04:47 字数 280 浏览 4 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(3

妄想挽回 2024-08-11 06:04:47

正如上面提到的
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!

度的依靠╰つ 2024-08-11 06:04:47

我使用了一个包装类来提供上下文

public DataContext Context = new DataContext(SqlConnectionString); //much simplified

I've used a wrapper class to provide a context along the lines of

public DataContext Context = new DataContext(SqlConnectionString); //much simplified
中二柚 2024-08-11 06:04:47

我通过在 Visual Studio 之外编辑 .dbml 文件(设计器似乎不允许访问 DatabaseAttribute)并在此处删除 name 属性来解决此问题:(

<Database Name="BadName" Class="OutputDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">

请注意,接受的答案不再正确:此属性覆盖我的连接字符串)

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:

<Database Name="BadName" Class="OutputDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">

(note that the accepted answer is no longer correct: this attribute was overriding my connection string)

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