linq to sql 项目移植后,数据库实体类需要重新创建?
项目中,使用LINQ to SQL 访问数据库,代码移植到其他机器上,每次需要重新生成dbml文件,有无方法只要更改app.config呢?
经过试验是可行的:
1、引用system.configuration.dll
2、修改app.config文件
<connectionStrings>
<add name="myDataBase"
connectionString="Data Source=5NZG9VWRCCBW0B6;Initial Catalog=db_DGB_Test;User ID=sa;Password=123"
providerName="System.Data.SqlClient" />
</connectionStrings>
3、修改Settting.designer.cs文件
public string db_DGB_TestConnectionString {
get {
//return ((string)(this["db_DGB_TestConnectionString"]));
return ((string)(ConfigurationManager.ConnectionStrings["myDataBase"].ConnectionString));
}
}
参考https://zhidao.baidu.com/question/363149027.html,
DbApp.designer.cs ,数据库实体类构造函数
public DbAppDataContext() :
base(global::DGB.User.Properties.Settings.Default.db_DGB_TestConnectionString, mappingSource)
{
OnCreated();
}
字符串在settting.designer.cs中定义
public string db_DGB_TestConnectionString {
get {
//return ((string)(this["db_DGB_TestConnectionString"]));
return ((string)(ConfigurationManager.ConnectionStrings["myDataBase"].ConnectionString));
}
}