将 dbml 中的连接字符串指向 app.config
我可以将 Dbml.designer.cs
中的连接字符串指向 app.conf
中的连接字符串吗?我编写了下面的代码,它成功指向 app.config
。
public leDataContext() :
base(ConfigurationManager.ConnectionStrings["leConnString"].ToString(), mappingSource)
{
OnCreated();
}
然而,每当我修改或添加一个表到 dbml 中时,它就会开始自动将该代码替换为
public leDataContext() :
base("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\My Projects\\App_Data\\le.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True", mappingSource)
{
OnCreated();
}
我已经扩展的“连接”选项。将“应用程序设置”设置为 False
Can I just point the connection string in Dbml.designer.cs
to the connectionstring in the app.conf
? I wrote the code below which it successfully point to the app.config
.
public leDataContext() :
base(ConfigurationManager.ConnectionStrings["leConnString"].ToString(), mappingSource)
{
OnCreated();
}
However whenever i modify or add a table into the dbml, it will start to auto replace that code into this
public leDataContext() :
base("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"D:\\My Projects\\App_Data\\le.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True", mappingSource)
{
OnCreated();
}
I have expanded the "Connection" option. Set "Application Settings" to False
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这更像是@Alex 答案的扩展。
第 1 步:将 .dbml 文件的连接属性设置为“none”。
第 2 步: 创建一个与其同名的新的单独部分类.dbml 文件的现有分部类。并使用无参数构造函数设置connectionString属性。
第 3 步:即将完成!最后,您需要在 app.config 文件中定义 connectionString,如下所示。
现在,您可以轻松地从 app.config 文件更改 connectionString,而无需重新编译代码,否则就会出现这种情况。
为什么我要创建一个单独的分部类?我不能编辑现有的 Dbml.designer.cs 文件吗?
不要手动修改 Dbml.designer.cs 文件,因为当您添加/编辑/删除表、存储过程等时,它会被重写。
This is more like an extension to @Alex's answer.
Step 1 : Set the connection property of your .dbml file to “none”.
Step 2 : Create a new separate partial class with the same name as that of the existing partial class for the .dbml file. And set the connectionString property by using the parameterless constructor.
Step 3 : Almost Done ! Lastly you need to define your connectionString in your app.config file, as shown below.
You can now easily change the connectionString from the app.config file without having to re-compile your code, which would be the case otherwise.
Why did I create a seperate partial class ? Can’t I edit the existing Dbml.designer.cs file ?
Don’t modify Dbml.designer.cs file manually, because it will be rewritten when you add/edit/delete a table, stored proc etc.
不要手动修改
Dbml.designer.cs
文件,因为当您按照您所说的编辑/添加表格等时,它将被重写。相反,将.dbml
设计器文件的Connection
属性设置为None
并添加具有无参数构造函数的分部类:Don't modify
Dbml.designer.cs
file manually, because it will be rewritten when you edit/add a table etc. as you said. Instead of this set theConnection
property for the.dbml
designer file toNone
and add a partial class with parameterless constructor: