所以這是我如何解決了:
修改了App.config中,如:
<connectionstrings configsource="DatabaseConnectionDetails.config" />;
,這是DatabaseConnectionDetails.config:
<connectionstrings>
<add name="networkingEntities" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=lucian;password=lucian;persist security info=True;database=networking"" providername="System.Data.EntityClient" />
<add name="networkingEntitiesAdmin" connectionstring="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=lucian;persist security info=True;database=networking"" providername="System.Data.EntityClient" />
</connectionstrings>
* 2 。 *在Model1.Context.tt模板中添加一個帶字符串參數的構造函數:
public <#=code.Escape(container)#>(string myString)
: base(myString)
{
<#
if (!loader.IsLazyLoadingEnabled(container))
{
#>
this.Configuration.LazyLoadingEnabled = false;
<#
}
#>
}
* 3。 *獲取連接字符串是這樣的:
string str = ConfigurationManager.ConnectionStrings["networkingEntitiesAdmin"].ConnectionString;
* *每當我想使用的情況下,我使用(例如):
networkingEntities net=new networkingEntities(str);
public List<utilizator> ListaUtilizatori()
{
var query = from u in net.utilizator
select u;
List<utilizator> users = new List<utilizator>();
try
{
users = query.ToList();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return users;
}
現在,我仍然有以發現我如何加密配置文件.... 謝謝大家的幫助...
使用[this constructor](http://msdn.microsoft.com/en-us/library/bb739017.aspx)按名稱選擇。或者,如果您使用EF5,[此構造函數](http://msdn.microsoft.com/en-us/library/gg679467(v = vs.103).aspx) – Aron