I'm using Entity Framework 4 for a simple app and would like to bake my connection credentials into the following connection string:
<connectionStrings>
<add name="MyEntities"
connectionString="metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=localhostDEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient" />
</connectionStrings>
However, the password (which I cannot change) contains an ampersand. ASP.NET throws:
Configuration Error: An error occurred while parsing EntityName. Line XX, position YYY.
If I replace the ampersand in the password with &
, I get a SqlException: Login failed for user 'myUser'.
Usually this trick works, but I'm guessing that something is failing because this is technically a connection string inside a connection string.
What should I do here? Most of my classes include code like:
using (var context = new MyEntities()) {
// do work
}
Update: It turns out that the credentials I am using are a domain account, so what I really need is Integrated Security=True
in the connection string rather than a password.
Encoding the ampersand as indicated in the accepted answer should work fine, though I haven't tested it.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…