问题 连接字符串不能与MySQL一起使用


我正在创造一个 REST API 运用 MySQL DB 在 visual studio 2015 in asp.net mvc 4.5。我已经完成了运行API所需的每一步 MySQL,但我得到了这个例外。

{“消息”:“发生了错误。”,“ExceptionMessage”:“初始化字符串的格式不符合从索引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在System.Data.Common.DbConnectionOptions.ParseInternal(Hashtable parsetable,String connectionString,Boolean buildChain,Hashtable synonyms,Boolean firstKey)\ r \ n在System.Data.Common.DbConnectionOptions..ctor(String connectionString,Hashtable synonyms definitions,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 System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__18中的.set_ConnectionString(String value)\ r \ n(DbConnection t,DbConnectionPropertyInterceptionContext1 c)\r\n at System.Data.Entity.Infrastructure.Interception.InternalDispatcher1.Dispatch [TTarget,TInterceptionContext](TTarget target,Action2 operation, TInterceptionContext interceptionContext, Action3执行,动作3 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在System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name,AppConfig config)\ r \ n在System.Data.Entity.Interial.Conit上的System.Data.Entity.Internal.LazyInternalConnection.Initialize()\ r \ n,在System.Data.Entity.Inter.RazyInternalContext.InitializeContext()中的System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel()\ r \ n \ r \ n在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)\ r \ n在System.Data.Entity.Internal.Linq.InternalSet中1.Initialize()\r\n at System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator()\ r \ n在System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator()\r\n at System.Collections.Generic.List1..ctor(IEnumerable的1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1源代码)\ r \ n在D:\ Work \ DOT NET API \ RestWithMySQL \ RestWithMySQL \ Controllers \ ProductsController.cs中的RestWithMySQL.Controllers.ProductsController.Get():第19行“}

我认为我的连接字符串有问题。我搜索过它但找不到确切的解决方案。

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

任何帮助将受到高度赞赏。


6994
2017-09-12 03:59


起源

不确定这是否是问题,但您是否尝试过更改 &quot; 至 "? - Steve Chambers


答案:


将连接字符串剥离到最低限度。添加选项并查看导致其失败的原因。例如 integrated security=true 不适用于MySQL。你需要添加一个 password 代替。

表格在这里: ASP.NET MVC 4 EF5与MySQL

尝试更接近这个:

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


8
2017-09-14 15:28



我已经改变了连接字符串,但现在我得到了这个 The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception.


你的连接字符串最后不应该有另一个引号吗?

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

进一步: 要使用MySQL提供程序,请考虑提供的这些步骤 在这个答案


1
2017-09-20 21:46





你错过了一个结束在连接字符串的末尾。


0
2017-09-21 01:28