连接字符串不适用于MySQL [英] Connection string not working with MySQL

查看:164
本文介绍了连接字符串不适用于MySQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用visual studio 2015 in asp.net mvc 4.5中的MySQL DB创建一个REST API.我已经完成了使用MySQL运行API所需的每个步骤,但是我遇到了这个异常.

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":发生错误.","ExceptionMessage":"初始化字符串的格式不符合从索引121开始的规范.","ExceptionType":"System.Data.Common.DbConnectionOptions.GetKeyValuePair(System.Data.Common.DbConnectionOptions.GetKeyValuePair(System.Data.Common.DbConnectionOptions.GetKeyValuePair(String connectionString,Int32 currentPosition,StringBuilder buffer,Boolean useOdbcRules,String& keyname,String& keyvalue)\ r \ n在System.Data处). Common.DbConnectionOptions.ParseInternal(可哈希表可分解,字符串connectionString,布尔值buildChain,哈希表同义词,布尔值firstKey)\ r \ n位于System.Data.Common.DbConnectionOptions..ctor(字符串连接字符串,哈希表同义词,布尔值useOdbcRules)\ r \ n在System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(字符串值)\ r \ n在MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(字符串connStr)\ r \ n在MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(字符串值)\ r \ n位于System.Data.Entity.Infras tructure.Interception.DbConnectionDispatcher.b__18(DbConnection t,DbConnectionPropertyInterceptionContext 1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch [TTarget,TInterceptionContext](TTarget目标,执行Action 2 operation, TInterceptionContext interceptionContext, Action 3,Action 3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext 1拦截上下文)\ r \ n在System.Data. Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection)\ r \ n在System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(字符串名称,AppConfig配置)\ r \ n在System.Data.Entity.Internal.LazyInternalConnection.Initialize ()\ r \ n在System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()\ r \ n在System.Data.Entity.Internal.LazyInternalContext.InitializeContext()\ r \ n在System.Data.Entity.Internal .InternalContext.GetEntitySetAndBaseTypeForType(类型entityType)\ r \ n在System.Data.Entity.Internal.Linq.InternalSet 1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet 1.GetEnumerator()\ r \ n在System.Data.Entity.Infrastructure.DbQuery 1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Collections.Generic.List 1 ..ctor(IEnumerable 1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable 1源)\ r \ n位于D:\ Work \ DOT NET API \ RestWithMySQL \ RestWithMySQL \ Controllers \ ProductsController.cs:line 19}中的RestWithMySQL.Controllers.ProductsController.Get()

{"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>

任何帮助将不胜感激.

推荐答案

将连接字符串降至最低.重新添加选项,然后查看导致失败的原因.例如,integrated security=true不适用于MySQL.您将需要添加password.

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.

在这里形成表格: 带有MySQL的ASP.NET MVC 4 EF5

尝试一些更接近的方法:

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=''"/>

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

这篇关于连接字符串不适用于MySQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆