Wednesday, September 20, 2017

REST API not working with MySQL

Leave a Comment

I am creating a REST API using MySQL DB in visual studio 2015 in asp.net mvc 4.5. I have done each and every step which in needed to run the API using MySQL, but I am getting this exception.

{"Message":"An error has occurred.","ExceptionMessage":"Format of the initialization string does not conform to specification starting at index 121.","ExceptionType":"System.ArgumentException","StackTrace":" at System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString, Int32 currentPosition, StringBuilder buffer, Boolean useOdbcRules, String& keyname, String& keyvalue)\r\n at System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable, String connectionString, Boolean buildChain, Hashtable synonyms, Boolean firstKey)\r\n at System.Data.Common.DbConnectionOptions..ctor(String connectionString, Hashtable synonyms, Boolean useOdbcRules)\r\n at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value)\r\n at MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)\r\n at MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__18(DbConnection t, DbConnectionPropertyInterceptionContext1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action2 operation, TInterceptionContext interceptionContext, Action3 executing, Action3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext1 interceptionContext)\r\n at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)\r\n at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config)\r\n at System.Data.Entity.Internal.LazyInternalConnection.Initialize()\r\n at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()\r\n at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\r\n at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\r\n at System.Data.Entity.Internal.Linq.InternalSet1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()\r\n at System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Collections.Generic.List1..ctor(IEnumerable1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\r\n at RestWithMySQL.Controllers.ProductsController.Get() in D:\Work\DOT NET API\RestWithMySQL\RestWithMySQL\Controllers\ProductsController.cs:line 19"}

I think there is a problem in my connection string. I have searched for it but couldn't find the exact solution.

<connectionStrings>     <!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-RestWithMySQL-20170911031521.mdf;Initial Catalog=aspnet-RestWithMySQL-20170911031521;Integrated Security=True" providerName="System.Data.SqlClient" />-->     <add name="ProductEntities" connectionString="metadata=res://*/ProductsModel.csdl|res://*/ProductsModel.ssdl|res://*/ProductsModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot; server=localhost;user id=root;database=accurate_dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="MySql.Data.MySqlClient"/>     <!--<remove name="LocalMySqlServer" /><add name="LocalMySqlServer" connectionString="" providerName="MySql.Data.MySqlClient" />--> </connectionStrings> 

Any help would be highly appreciated.

3 Answers

Answers 1

Strip down your connection string to the bare minimum. Add options back and see what causes it to fail. For example integrated security=true is not going to work with MySQL. You will need to add a password instead.

Form here: ASP.NET MVC 4 EF5 with MySQL

Try something closer to this:

<add name="DefaultConnection" providerName="MySql.Data.MySqlClient" connectionString="Data Source=localhost;port=3306;Initial Catalog=api_db;User Id=root;password=''"/>

Answers 2

Shouldn't your connection string have another quote in it at the end?

<connectionStrings>     <add name="ProductEntities" connectionString="metadata=res://*/ProductsModel.csdl|res://*/ProductsModel.ssdl|res://*/ProductsModel.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot; server=localhost;user id=root;database=accurate_dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="MySql.Data.MySqlClient"/> </connectionStrings> 

Further: In order to use the MySQL provider, consider these steps provided in this answer

Answers 3

You are missing a closing &quot; at the end of your connection string.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment