locked
Entity Framework + Spaces in Connection String RRS feed

  • Question

  • We have found what appears to be a bug in the parsing of connection strings in entity framework (may only be when using code-first), when a value in a connection string contains spaces.  In our case, it was our SQL server password.

    Take the following connection string for example:

      <connectionStrings>
        <add name="myConnection" connectionString="Server=localhost; Initial Catalog=mydb; User ID=myuser; PWD=&quot;my password&quot;" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    This connection string is perfectly valid for a straight SQL connection, but when attempting to use it to create our context (var context = new MyDbContext("name=myConnection")), it will throw an exception "Format of the initialization string does not conform to specification starting at index...".

    This bug may only be relevant when using code first, because of the allowance of using regular SQL connection strings (no metadata provided).  We have found that when providing this string to the datacontext constructor, a connection is eventually made where the connection string is flushed out to include some metadata automatically, and when trying to parse this new connection string is where the exception is thrown.

    For now we simply changed our password to not contain spaces...

    Tuesday, July 10, 2012 1:41 AM

Answers

All replies

  • You can't have spaces in a sql password.
    Tuesday, July 10, 2012 8:55 AM
  • Well, as I mentioned, the password with the spaces has worked everywhere else (both in code as well as logging into database tools such as sql management tools)... so I can't agree with you there unless you can back it up with more information.

    We had to step through the code using reflector to figure out what was going on, and we think this potential bug would actually apply to any attempt at using additional quotes in the string, which means that no values could support spaces, not just the password.  

    I should mention we are using Entity Framework 4.2, and re-iterate that this problem likely only exists using code first.  The framework appears to build up a connection string internally to include metadata, is passed to System.Data.Common.DbConnectionOptions to be parsed, and throws the format exception in GetKeyValuePair().  

    The problem could be with the parsing, it could be when the string is expanded, etc.  If there is a format that will work in all cases it would be good to know.  The documentation states that any value that contains double quotes should be wrapped in double quotes, but when trying this our original connection string failed.

    Tuesday, July 10, 2012 2:28 PM
  • Hi,

    Not sure in which method it happens but it seems you really ensured this is a EF. Code first perhaps needs to do that as it can have first to ensure wether or not the database is already available before creating it as an optional step so it likely does some processing on the connection string ?

    According to http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions , the bug report site for EF is now http://connect.microsoft.com/VisualStudio


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    • Marked as answer by Yatser Tuesday, July 10, 2012 3:55 PM
    Tuesday, July 10, 2012 3:36 PM
  • Thanks! I'll go post it there and mark yours as answer until they can confirm whether it is a bug or not...
    Tuesday, July 10, 2012 3:55 PM