将dbml中的连接字符串指向app.config [英] Point connectionstring in dbml to app.config
问题描述
是否可以仅将Dbml.designer.cs
中的连接字符串指向app.conf
中的连接字符串?我写了下面的代码,它成功地指向了app.config
.
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();
}
但是,每当我在dbml中修改表或将表添加到dbml中时,它将开始自动将该代码替换为
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();
}
我已经扩展了连接"选项.将应用程序设置"设置为False
I have expanded the "Connection" option. Set "Application Settings" to False
推荐答案
请勿手动修改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 the Connection
property for the .dbml
designer file to None
and add a partial class with parameterless constructor:
public partial class leDataContext
{
public leDataContext() :
base(ConfigurationManager.ConnectionStrings["leConnString"].ToString())
{
OnCreated();
}
}
这篇关于将dbml中的连接字符串指向app.config的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!