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="server=server.example.com;user id=username;password=pass;persistsecurityinfo=True;database=dbname;convertzerodatetime=True;characterset=utf8"" 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).
SqlConnectionStringBuilderis for MS's ownSqlConnectionconnections, not for MySQL or other providers. You'll need to look for an alternative for MySQL.