views:

65

answers:

3

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=&quot;Data Source=localhost\DEV;Initial Catalog=MyDB;UserId=myUser;Password=jack&jill;MultipleActiveResultSets=True&quot;" 
         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 &amp;, 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.

+1  A: 

You'll need to use escape sequences like you would for any XML document, which is all the .config files are.

  • Ampersand = & = &amp;
  • Greater Than = > = &gt;
  • Less Than = < = &lt;
  • Apostrophe = ' = &apos;
  • Quote = " = &quot;

You can also use the CDATA tag so that you can use these illegal characters

<![CDATA[ and ends with ]]>

<connectionStrings>
    <add name="MyEntities" connectionString="
        metadata=res://*/MyDataModel.csdl|res://*/MyDataModel.ssdl|res://*/MyDataModel.msl;
        provider=System.Data.SqlClient;
        provider connection string=&quot;
        Data Source=localhost\DEV;
        Initial Catalog=MyDB;UserId=myUser;
        Password=<![CDATA[jack&jill]]>;
        MultipleActiveResultSets=True&quot;" 
        providerName="System.Data.EntityClient" />
</connectionStrings>
hunter
I already tried that, as noted in my question. It seems that the wrong password is sent along to the Entity Framework.
That really should be all you need to get it to work.
hunter
Also, make you've tried logging into that SQL Server instance using the credentials in your connectionstring outside of your application
hunter
@hunter - Right on about testing the credentials. I was led to believe that these were SQL server credentials, but they are, in fact, Active Directory credentials. Oooops. Thanks for the "make sure the computer is plugged in" suggestions. We all need that sometimes.
Additionally, let me just comment to say that escaping XML characters usually does work fine. For example, in an <identity impersonate='true' /> tag in a web.config file. However, I don't think you are allowed to put a CDATA section inside an XML attribute (XML 1.0 spec section 3.3.3).
A: 

Maybe encrypting the password using an RsaProtectedConfigurationProvider would allow the password to be unencrypted into a form that can be escaped?

KeithS
A: 

…this is technically a connection string inside a connection string

Have you tried escaping twice?

&amp;amp;
Sidnicious