1

I am trying to connect to MySQL database using Entity Framework 5. Everything works fine when using the connection string supplied by app.config:

<add name="MHEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=server.example.com;user id=username;password=pass;persistsecurityinfo=True;database=dbname;convertzerodatetime=True;characterset=utf8&quot;" providerName="System.Data.EntityClient" />

However, I need to create the connection in code (I want to be able to supply the db password at runtime). So far I have the following code:

      SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(@"Convert Zero Datetime=true;");
        sqlBuilder.DataSource = "server.example.com";
        sqlBuilder.InitialCatalog = "dbname";
        sqlBuilder.UserID = "username";
        sqlBuilder.Password = "password";
        sqlBuilder.IntegratedSecurity = true;
      //  sqlBuilder.Add("convertzerodatetime", "true");

        string providerString = sqlBuilder.ToString();

        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();

        entityBuilder.Metadata = @"res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl";
        entityBuilder.Provider = "MySql.Data.MySqlClient";
        entityBuilder.ProviderConnectionString = providerString;

        ConnString = entityBuilder.ConnectionString;

        MHEntities context = new MHEntities(ConnString);

And that works. But as soon as I uncomment the one line in the code above, I get an exception:

Keyword not supported: 'convertzerodatetime'. I tried "Convert Zero Datetime" with same results.

I need this setting (Convert Zero Datetime=true), because without it the app fails when it tries to load datetime values from the database with all zeros (I have no control over those).

1
  • SqlConnectionStringBuilder is for MS's own SqlConnection connections, not for MySQL or other providers. You'll need to look for an alternative for MySQL. Commented May 1, 2015 at 17:17

2 Answers 2

2

as error message says, SqlConnectionStringBuilder does not support the convertzerodatetime keyword. Consider using the MySqlConnectionStringBuilder instead.

Sign up to request clarification or add additional context in comments.

Comments

0

'Convert Zero Datetime' works on EF 6, can you update your library? Try to change your web.config from 'convertzerodatetime=True' to 'Convert Zero Datetime=True'.

<add name="MHEntities" connectionString="metadata=res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=server.example.com;user id=username;password=pass;persistsecurityinfo=True;database=dbname;characterset=utf8;Convert Zero Datetime=True&quot;" providerName="System.Data.EntityClient" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.