【问题标题】:Connection string not working with MySQL连接字符串不适用于 MySQL
【发布时间】:2018-02-20 09:39:46
【问题描述】:

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

{"Message":"发生错误。","ExceptionMessage":"初始化字符串的格式不符合从索引 121 开始的规范。","ExceptionType":" System.ArgumentException","StackTrace":" 在 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 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 在 MySql.Data.MySqlClient.MySqlConnectionStringBuilder..ctor(String connStr)\r\n 在 MySql.Data.MySqlClient.MySqlConnection.set_ConnectionString(String value)\ r\n 在 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 目标,Action2 operation, TInterceptionContext interceptionContext, Action3 正在执行,Action3 executed)\r\n at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.SetConnectionString(DbConnection connection, DbConnectionPropertyInterceptionContext1 interceptionContext)\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(Type entityType)\r\n 在 System.Data.Entity.Internal.Linq.InternalSet1.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 (IEnumerable1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 来源)\r\n 在 D:\Work\DOT NET API\RestWithMySQL\RestWithMySQL\Controllers\ProductsController.cs:line 19 中的 RestWithMySQL.Controllers.ProductsController.Get()"}

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

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

任何帮助将不胜感激。

【问题讨论】:

  • 不确定这是否是问题,但您是否尝试将&amp;quot; 更改为"

标签: mysql entity-framework


【解决方案1】:

尽量减少连接字符串。重新添加选项,看看是什么导致它失败。例如integrated security=true 不能与 MySQL 一起工作。您需要添加 password

这里的表格: ASP.NET MVC 4 EF5 with MySQL

尝试更接近此的方法:

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

【讨论】:

  • 我已经更改了连接字符串,但现在我得到了这个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.
【解决方案2】:

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

<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 提供程序,请考虑提供的这些步骤in this answer

【讨论】:

    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2017-10-15
      • 1970-01-01
      • 2016-09-06
      • 2015-11-23
      • 2020-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 2017-09-28
      相关资源
      最近更新 更多